MongoDb bulk operation get id

ぃ、小莉子 提交于 2020-02-04 11:04:06

问题


I want to perform bulk operation via MongoDb. How to get array of Ids that will be returned after it?

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

update: I found the following solution - save maximum ObjectId of mongo collection,

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?


回答1:


You can insert items in bulk using the new driver with 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. For example:

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


来源:https://stackoverflow.com/questions/31724463/mongodb-bulk-operation-get-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!