问题
Think of this MongoDB document:
{_id:123, "food":[ "apple", "banana", "mango" ]}
Question: How to get the position of mango
in food?
The query should return 2
in above, and don't return the whole document.
Please kindly show the working query.
回答1:
Starting from MongoDB version 3.4 we can use the $indexOfArray operator to return the index at which a given element can be found in the array.
$indexOfArray
takes three arguments. The first is the name of the array field prefixed with $
sign.
The second is the element and the third optional is the index to start the search at. $indexOfArray
returns the first index at which the element is found if the index to start the search at is not specified.
Demo:
> db.collection.insertOne( { "_id" : 123, "food": [ "apple", "mango", "banana", "mango" ] } )
{ "acknowledged" : true, "insertedId" : 123 }
> db.collection.aggregate( [ { "$project": { "matchedIndex": { "$indexOfArray": [ "$food", "mango" ] } } } ] )
{ "_id" : 123, "matchedIndex" : 1 }
> db.collection.aggregate( [ { "$project": { "matchedIndex": { "$indexOfArray": [ "$food", "mango", 2 ] } } } ] )
{ "_id" : 123, "matchedIndex" : 3 }
> db.collection.aggregate( [ { "$project": { "matchedIndex": { "$indexOfArray": [ "$food", "apricot" ] } } } ] )
{ "_id" : 123, "matchedIndex" : -1 }
回答2:
There really is no other way ( "server side" ) than using mapReduce:
db.collection.mapReduce(
function() {
emit(this._id, this.food.indexOf("mango"));
},
function() {}, // reducer never gets called since all _id is unique
{
"out": { "inline": 1 },
"query": { "food": "mango" }
}
)
It is the only thing that will return something else in a modified form other than the document itself, as well as using the needed JavaScript evaluation in order to determine the answer,
There is unfortunately no "native" operator that will do this.
Unless you need this for real aggregation purposes, then it is better to just do a similar "array index match" in native code in your client when dealing on a "per document" basis.
回答3:
In mongo shell (in Robomongo also) I would do the following:
var food = db.getCollection('yourCollection').findOne({_id: '123'}).food;
print('Index of mango: ' + food.indexOf('mango'));
or you can save this code in any_file.js
and then run from command line:
mongo your_db any_file.js
It will produce something like that:
MongoDB shell version: 2.4.9
connecting to: localhost:27017/your_db
Index of mango: 2
来源:https://stackoverflow.com/questions/33100750/get-index-of-given-element-in-array-field-in-mongodb