I want to insert bulk data into a MongoDB. Later on I want to find exactly the bulk I inserted.

In MySQL I would have done:

 SELECT * FROM data WHERE id >= start_id AND id <= end_id

How can I fix this in MongoDB?


If you know start _id and end _id then you can retrieve the bulk you inserted using query like this:

db.yourCollection.find({_id:{$gt:ObjectId("56e13d5aed63a8477a7e2e78"), $lt: ObjectId("570f5a27b2d1a50996d3a0f0")}})

Replace the default mongodb ObjectIds with your _id fields.

Also, if the _id you are using is default mongodb ObjectId then you can easily compare them using above query since mongodb ObjectIds are monotonically increasing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.