简体   繁体   English

MongoDb批量操作获取id

[英]MongoDb bulk operation get id

I want to perform bulk operation via MongoDb.我想通过 MongoDb 执行批量操作。 How to get array of Ids that will be returned after it?如何获取之后将返回的ID数组?

Can i perform single-operation insert faster without using bulk ?我可以在不使用批量的情况下更快地执行单操作插入吗? Can you advise me some other approach ?你能告诉我一些其他的方法吗? I'm using C# mongoDb driver 2.0 and MongoDb v. 3.0.2我正在使用C# mongoDb driver 2.0MongoDb v. 3.0.2

update: I found the following solution - save maximum ObjectId of mongo collection,更新:我找到了以下解决方案 - 保存 mongo 集合的最大ObjectId

db.col.find().sort({_id:-1}).limit(1).pretty()

and do the same after insert So we will get the range of inserted documents, does it make a sense?并在插入后做同样的事情所以我们会得到插入文档的范围,这有意义吗?

You can insert items in bulk using the new driver with InsertManyAsync .您可以使用带有InsertManyAsync的新驱动程序批量插入项目。 If you want the Ids that the driver generated for these items you can simply get them out of the items themselves after they are inserted.如果您想要驱动程序为这些项目生成的 ID,您可以在插入项目后简单地将它们从项目本身中取出。 For example:例如:

Hamster[] hamsters = { new Hamster { Name = "Vaska" }, new Hamster { Name = "Petzka" } };
await collection.InsertManyAsync(hamsters);
var insertedIDs = hamsters.Select(_ => _.Id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM