MongoDB根据指定字段的值选择文档



MongoDB - userModel(简化)

{
_id: "exampleid1",
name: "examplename1",
following: [
{
_id: "exampleid2",
name: "examplename2"
},
{
_id: "exampleid3",
name: "examplename3"
}
],
followers: [
{
_id: "exampleid4",
name: "examplename4"
},
{
_id: "exampleid5",
name: "examplename5"
}
]
}

嘿,我正在建立一个社交媒体平台,我需要从数据库中获取我所关注的用户。

我尝试了下面的查询,但是没有得到任何结果:

User.find( { "followers": { _id: "exampleid1"} } )

尝试将文档选择({_id: "exampleid1"})与投影({followers: 1, _id: 0})分开:

db.collection.find({
_id: "exampleid1"
},
{
followers: 1,
_id: 0
})

看看它在操场的例子中是如何工作的

最新更新