如何在MongoDB中以索引方式检索数组值?在下面的查询中,我想要Computers
作为答案:
> db.customers.find({}, { likes : { $slice: 1}}).pretty()
{
"_id" : ObjectId("57709662a93160be074de74a"),
"name" : "John",
"age" : "40",
"likes" : [
"Computers"
]
}
答案 0 :(得分:0)
这就是你要找的东西
db.customers.aggregate([{$project:{_id:0,selected:{$slice:["$likes", 1]}}},{$unwind:"$selected"}]);