Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get position of selected document in collection [mongoDB]

How to get position (index) of selected document in mongo collection?

E.g. this document: db.myCollection.find({"id":12345}) has index 3 in myCollection

myCollection:     id: 12340, name: 'G'     id: 12343, name: 'V'     id: 12345, name: 'A'     id: 12348, name: 'N' 
like image 790
Saint Avatar asked May 30 '12 09:05

Saint


People also ask

How do I find the index of a document in MongoDB?

You can find all the available indexes in a MongoDB collection by using the getIndexes method. This will return all the indexes in a specific collection. Result: The output contains the default _id index and the user-created index student name index.

How do I capture a specific field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

What does find () do in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.

How retrieve data from collections in MongoDB?

You can use read operations to retrieve data from your MongoDB database. There are multiple types of read operations that access the data in different ways. If you want to request results based on a set of criteria from the existing set of data, you can use a find operation such as the find() or findOne() methods.


2 Answers

If your requirement is to find the position of the document irrespective of any order, that is not possible as MongoDb does not store the documents in specific order. However,if you want to know the index based on some field, say _id , you can use this method.

If you are strictly following auto increments in your _id field. You can count all the documents that have value less than that _id, say n , then n + 1 would be index of the document based on _id.

n = db.myCollection.find({"id": { "$lt" : 12345}}).count() ; 

This would also be valid if documents are deleted from the collection.

like image 91
DhruvPathak Avatar answered Oct 20 '22 01:10

DhruvPathak


As far as I know, there is no single command to do this, and this is impossible in general case (see Derick's answer). However, using count() for a query done on an ordered id value field seems to work. Warning: this assumes that there is a reliably ordered field, which is difficult to achieve in a concurrent writer case. In this example _id is used, however this will only work with a single writer case.:

MongoDB shell version: 2.0.1 connecting to: test > use so_test switched to db so_test > db.example.insert({name: 'A'}) > db.example.insert({name: 'B'}) > db.example.insert({name: 'C'}) > db.example.insert({name: 'D'}) > db.example.insert({name: 'E'}) > db.example.insert({name: 'F'}) > db.example.find() { "_id" : ObjectId("4fc5f040fb359c680edf1a7b"), "name" : "A" } { "_id" : ObjectId("4fc5f046fb359c680edf1a7c"), "name" : "B" } { "_id" : ObjectId("4fc5f04afb359c680edf1a7d"), "name" : "C" } { "_id" : ObjectId("4fc5f04dfb359c680edf1a7e"), "name" : "D" } { "_id" : ObjectId("4fc5f050fb359c680edf1a7f"), "name" : "E" } { "_id" : ObjectId("4fc5f053fb359c680edf1a80"), "name" : "F" } > db.example.find({_id: ObjectId("4fc5f050fb359c680edf1a7f")}) { "_id" : ObjectId("4fc5f050fb359c680edf1a7f"), "name" : "E" } > db.example.find({_id: {$lte: ObjectId("4fc5f050fb359c680edf1a7f")}}).count() 5 >  

This should also be fairly fast if the queried field is indexed. The example is in mongo shell, but count() should be available in all driver libs as well.

like image 23
jhonkola Avatar answered Oct 20 '22 02:10

jhonkola