如何查找具有 objects 数组且属性在 mongodb 中的所有文档?
How to find all docs having array with objects with properties in mongodb?
我有 collection:
{
"_id": "1",
"userId": "2",
"userName": "A",
}
{
"_id": "2",
"userId": "3",
"userName": "B",
}
{
"_id": "4",
"userId": "5",
"userName": "C",
}
{
"_id": "6",
"userId": "7",
"userName": "D",
}
我需要提出一些请求,例如:
db.users.find([
{
"userId": "2",
"userName": "A",
},
{
"userId": "3",
"userName": "B",
},
{
"userId": "5",
"userName": "C",
}])
我想得到:
{
"_id": "1",
"userId": "2",
"userName": "A",
}
{
"_id": "2",
"userId": "3",
"userName": "B",
}
{
"_id": "4",
"userId": "5",
"userName": "C",
}
抱歉,这里的查找请求不太可读。 Whosebug 认为它的代码如此之多,而文字却寥寥无几。
你可以试试这个
db.collection.find({
$or: [
{ userId: "2", userName: "A" },
{ userId: "3", userName: "B" }
]
})
我有 collection:
{
"_id": "1",
"userId": "2",
"userName": "A",
}
{
"_id": "2",
"userId": "3",
"userName": "B",
}
{
"_id": "4",
"userId": "5",
"userName": "C",
}
{
"_id": "6",
"userId": "7",
"userName": "D",
}
我需要提出一些请求,例如:
db.users.find([
{
"userId": "2",
"userName": "A",
},
{
"userId": "3",
"userName": "B",
},
{
"userId": "5",
"userName": "C",
}])
我想得到:
{
"_id": "1",
"userId": "2",
"userName": "A",
}
{
"_id": "2",
"userId": "3",
"userName": "B",
}
{
"_id": "4",
"userId": "5",
"userName": "C",
}
抱歉,这里的查找请求不太可读。 Whosebug 认为它的代码如此之多,而文字却寥寥无几。
你可以试试这个
db.collection.find({
$or: [
{ userId: "2", userName: "A" },
{ userId: "3", userName: "B" }
]
})