mongodb查询返回内嵌符合条件的文档

时间:2025-04-03 20:03:31

db.T_Forum_Thread.find({ "ThreadReply.ReplyContent" : /范甘迪/ }, { "ThreadReply.$" : 1, "ThreadContent" : 1 })

重点在于返回字段:ThreadReply.$:1

如果内嵌的数组有多个符合条件只返回了1条,,,待继续深究

db.test.aggregate(
{"$unwind":"$favorite_shards"},
{"$match":
{"favorite_shards.sid":
{"$in": [NumberLong(578), NumberLong(577)]}
}
},
{"$group":
{"_id": "$_id", "favorite_shards":
{'$push': "$favorite_shards"}
}
})

db.web_mem_favorites.aggregate(
{"$project":{"favorite_shards":"$favorite_shards"}},
{"$unwind":"$favorite_shards"},
{"$match":{"favorite_shards.sid": NumberLong(577)}}
)