查找包含匹配两个(或更多)属性的子文档的文档
Find documents with sub-documents matching both of two (or more) properties
在 Node with Mongoose 中,我想在集合 Content
中找到一个对象。它有一个名为 users
的子文档列表,该列表具有属性 stream
、user
和 added
。我这样做是为了在 users.user
字段中获取某个用户的 _id
属性 的所有文档。
Content.find( { 'users.user': user._id } ).sort( { 'users.added': -1 } )
这似乎有效(尽管我不确定 .sort
是否真的在这里有效。但是,我想匹配 两个 字段,如下所示:
Content.find( { 'users.user': user._id, 'users.stream': stream } } ).sort( { 'users.added': -1 } )
这似乎行不通。正确的做法是什么?
这是一个示例文档
{
"_id" : ObjectId("551c6b37859e51fb9e9fde83"),
"url" : "https://www.youtube.com/watch?v=f9v_XN7Wxh8",
"title" : "Playing Games in 360°",
"date" : "2015-03-10T00:19:53.000Z",
"author" : "Econael",
"description" : "Blinky is a proof of concept of enhanced peripheral vision in video games, showcasing different kinds of lens projections in Quake (a mod of Fisheye Quake, using the TyrQuake engine).\n\nDemo and additional info here:\nhttps://github.com/shaunlebron/blinky\n\nThanks to @shaunlebron for making this very interesting proof of concept!\n\nSubscribe: http://www.youtube.com/subscription_center?add_user=econaelgaming\nTwitter: https://twitter.com/EconaelGaming",
"duration" : 442,
"likes" : 516,
"dislikes" : 13,
"views" : 65568,
"users" : [
{
"user" : "54f6688c55407c0300b883f2",
"added" : 1427925815190,
"_id" : ObjectId("551c6b37859e51fb9e9fde84"),
"tags" : []
}
],
"images" : [
{
"hash" : "1ab544648d7dff6e15826cda7a170ddb",
"thumb" : "...",
"orig" : "..."
}
],
"tags" : [],
"__v" : 0
}
使用 $elemMatch 运算符对嵌入文档数组指定多个条件:
Content.find({"users": {$elemMatch: {"user": user.id, "stream": stream}}});
在 Node with Mongoose 中,我想在集合 Content
中找到一个对象。它有一个名为 users
的子文档列表,该列表具有属性 stream
、user
和 added
。我这样做是为了在 users.user
字段中获取某个用户的 _id
属性 的所有文档。
Content.find( { 'users.user': user._id } ).sort( { 'users.added': -1 } )
这似乎有效(尽管我不确定 .sort
是否真的在这里有效。但是,我想匹配 两个 字段,如下所示:
Content.find( { 'users.user': user._id, 'users.stream': stream } } ).sort( { 'users.added': -1 } )
这似乎行不通。正确的做法是什么?
这是一个示例文档
{
"_id" : ObjectId("551c6b37859e51fb9e9fde83"),
"url" : "https://www.youtube.com/watch?v=f9v_XN7Wxh8",
"title" : "Playing Games in 360°",
"date" : "2015-03-10T00:19:53.000Z",
"author" : "Econael",
"description" : "Blinky is a proof of concept of enhanced peripheral vision in video games, showcasing different kinds of lens projections in Quake (a mod of Fisheye Quake, using the TyrQuake engine).\n\nDemo and additional info here:\nhttps://github.com/shaunlebron/blinky\n\nThanks to @shaunlebron for making this very interesting proof of concept!\n\nSubscribe: http://www.youtube.com/subscription_center?add_user=econaelgaming\nTwitter: https://twitter.com/EconaelGaming",
"duration" : 442,
"likes" : 516,
"dislikes" : 13,
"views" : 65568,
"users" : [
{
"user" : "54f6688c55407c0300b883f2",
"added" : 1427925815190,
"_id" : ObjectId("551c6b37859e51fb9e9fde84"),
"tags" : []
}
],
"images" : [
{
"hash" : "1ab544648d7dff6e15826cda7a170ddb",
"thumb" : "...",
"orig" : "..."
}
],
"tags" : [],
"__v" : 0
}
使用 $elemMatch 运算符对嵌入文档数组指定多个条件:
Content.find({"users": {$elemMatch: {"user": user.id, "stream": stream}}});