我试图在没有任何成功的情况下查询嵌入式文档数组的id。很高兴知道如何使用其ID'来查询嵌入式文档数组。
下面是我的名为sp
的集合{ "_id":ObjectId("54bd949a44ae90d7e1473581"),"orgName":"org name","towns":[{"_id":ObjectId("54bd7829104d630153f66082"),"status":"SUBMITTED","contact":{"name":"con name","email":"con@email.com","phone":"34567890123"}}]}
{ "_id":ObjectId("54bd94ac44ae90d7e1473582"),"orgName":"sp3 org name","towns":[{"_id":ObjectId("54bd7829104d630153f66082"),"status":"SUBMITTED","contact":{"name":"sp3 contact name","email":"sp3con@email.com","phone":"45678903"}}]}
{ "_id":ObjectId("54bea67c44ae36e60e584d46"),"orgName":"org name","towns":[{"_id":ObjectId("54bd7829104d630153f66082"),"status":"SUBMITTED","contact":{"name":"con name","email":"con@email.com","phone":"34567890123"}}]}
以下查询返回null。我希望它能归还所有文件。
db.sp.find({'towns.id':{$in:['54bd7829104d630153f66082']}})
db.sp.find({'towns.id':{$in:[ObjectId("54bd7829104d630153f66082")]}})
以下查询返回所有三个文档
db.sp.find({'towns.status':{$in:['SUBMITTED']}})
答案 0 :(得分:0)
正如我在评论中所说,你错过了下划线。
db.sp.find({'towns._id':ObjectId("54bd7829104d630153f66082")})
^
此处您不需要$in
运算符。