spring 查询数组的嵌入文档

spring query embedded document of arrays

我正在尝试查询嵌入式文档数组的 ID,但没有成功。很高兴知道如何使用其 'id'.

查询嵌入式文档数组

下面是我的 collection 命名为 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"}}]}

以下查询 return 为空。我希望它 return 所有文件。

db.sp.find({'towns.id':{$in:['54bd7829104d630153f66082']}})
db.sp.find({'towns.id':{$in:[ObjectId("54bd7829104d630153f66082")]}})

下面查询returns所有三个文档

db.sp.find({'towns.status':{$in:['SUBMITTED']}})

正如我在评论中所说,您缺少下划线。

db.sp.find({'towns._id':ObjectId("54bd7829104d630153f66082")})
                   ^

此外,您在这里不需要 $in 运算符。