我知道$in
操作符,它似乎在搜索数组中某个项的存在,但我只想在该项位于数组的第一个位置时找到匹配项.
例如:
{
"_id" : ObjectId("0"),
"imgs" : [
"http://foo.jpg",
"http://bar.jpg",
"http://moo.jpg",
]
},
{
"_id" : ObjectId("1"),
"imgs" : [
"http://bar.jpg",
"http://foo.jpg",
"http://moo.jpg",
]
}
我在寻找一个类似于:
db.products.find({"imgs[0]": "http://foo.jpg"})
这将/应该返回ObjectId("0")
,而不是ObjectId("1")
,因为它只判断数组中的第一个图像.
如何做到这一点?我知道我可以创建一个单独的字段,其中包含一个firstImg
的字符串,但这并不是我想要的.