MongoDB:无法找到所需的记录
我是新来的MongoDB:无法找到所需的记录,mongodb,Mongodb,我是新来的MongoDB。我的MongoDB中有以下收藏。我的问题是,我无法生成一个find命令,该命令根据提供的ID或名称提供所需的设施 查找我已尝试但不起作用的命令:- db.mgh_facilities.find({facilities: {$elemMatch: {name: "Foreign exchange assistance"}}}). 对我来说,是归还所有的记录。它不应该只是返回{“id”:“11”,“name”:“外汇援助”} 我的收藏:- { "_id" : Ob
MongoDB
。我的MongoDB中有以下收藏。我的问题是,我无法生成一个find命令,该命令根据提供的ID或名称提供所需的设施
查找我已尝试但不起作用的命令:-
db.mgh_facilities.find({facilities: {$elemMatch: {name: "Foreign exchange assistance"}}}).
对我来说,是归还所有的记录。它不应该只是返回{“id”:“11”,“name”:“外汇援助”} 我的收藏:-
{
"_id" : ObjectId("548acc28ae6ff1c0fd1d7470"),
"responseCode" : "true",
"facilities" : [
{
"id" : "11",
"name" : "Foreign exchange assistance"
},
{
"id" : "12",
"name" : "Assistance with luggage on request"
},
{
"id" : "13",
"name" : "24 hours power back-up"
},
{
"id" : "14",
"name" : "A/C Power Backup Available"
},
{
"id" : "15",
"name" : "swimming pool"
},
{
"id" : "35",
"name" : "shoe cleaning service"
},
{
"id" : "36",
"name" : "Smoke detectors"
},
{
"id" : "37",
"name" : "Fire Extinguishers in each room"
},
{
"id" : "38",
"name" : "Pest Control"
},
{
"id" : "39",
"name" : "Conference / Banquet Hall"
},
{
"id" : "45",
"name" : "Restaurant"
},
{
"id" : "53",
"name" : "Anti-slip ramps"
},
{
"id" : "56",
"name" : "Tea/ Coffee Maker in the Rooms"
},
{
"id" : "59",
"name" : "Wi Fi Internet"
}
]
}
你可以试试这个
db.mgh_facilities.aggregate([
{
$unwind:"$facilities"
},
{
$match:{"facilities.name": "Foreign exchange assistance"}
},{
$project:{
_id:0,
facilities:1
}
}
])
结果:
{
"result" : [
{
"facilities" : {
"id" : "11",
"name" : "Foreign exchange assistance"
}
}
],
"ok" : 1
}
你可以试试这个
db.mgh_facilities.aggregate([
{
$unwind:"$facilities"
},
{
$match:{"facilities.name": "Foreign exchange assistance"}
},{
$project:{
_id:0,
facilities:1
}
}
])
结果:
{
"result" : [
{
"facilities" : {
"id" : "11",
"name" : "Foreign exchange assistance"
}
}
],
"ok" : 1
}
你可以试试这个
db.mgh_facilities.aggregate([
{
$unwind:"$facilities"
},
{
$match:{"facilities.name": "Foreign exchange assistance"}
},{
$project:{
_id:0,
facilities:1
}
}
])
结果:
{
"result" : [
{
"facilities" : {
"id" : "11",
"name" : "Foreign exchange assistance"
}
}
],
"ok" : 1
}
你可以试试这个
db.mgh_facilities.aggregate([
{
$unwind:"$facilities"
},
{
$match:{"facilities.name": "Foreign exchange assistance"}
},{
$project:{
_id:0,
facilities:1
}
}
])
结果:
{
"result" : [
{
"facilities" : {
"id" : "11",
"name" : "Foreign exchange assistance"
}
}
],
"ok" : 1
}
“查找”命令已找到包含名称与搜索匹配的设施对象的文档。它不知道您只希望看到负责该文档的单个子文档与您的搜索条件匹配 您可以告诉mongo,您希望使用positional$操作符查看该元素
db.mgh_facilities.find({"facilities.name": "Foreign exchange assistance"}, {"facilities.$": true})
“查找”命令已找到包含名称与搜索匹配的设施对象的文档。它不知道您只希望看到负责该文档的单个子文档与您的搜索条件匹配 您可以告诉mongo,您希望使用positional$操作符查看该元素
db.mgh_facilities.find({"facilities.name": "Foreign exchange assistance"}, {"facilities.$": true})
“查找”命令已找到包含名称与搜索匹配的设施对象的文档。它不知道您只希望看到负责该文档的单个子文档与您的搜索条件匹配 您可以告诉mongo,您希望使用positional$操作符查看该元素
db.mgh_facilities.find({"facilities.name": "Foreign exchange assistance"}, {"facilities.$": true})
“查找”命令已找到包含名称与搜索匹配的设施对象的文档。它不知道您只希望看到负责该文档的单个子文档与您的搜索条件匹配 您可以告诉mongo,您希望使用positional$操作符查看该元素
db.mgh_facilities.find({"facilities.name": "Foreign exchange assistance"}, {"facilities.$": true})
查询对我来说没问题。请编辑您的问题以添加您获得的输出和预期的输出。对我来说,它将返回所有记录。它不应该只是返回{“id”:“11”,“name”:“Foreign exchange assistance”},查询对我来说可以。请编辑您的问题,添加您获得的输出和预期的输出。对我来说,它会返回所有记录。它不应该只是返回{“id”:“11”,“name”:“Foreign exchange assistance”},查询对我来说可以。请编辑您的问题,添加您获得的输出和预期的输出。对我来说,它会返回所有记录。它不应该只是返回{“id”:“11”,“name”:“Foreign exchange assistance”},查询对我来说可以。请编辑您的问题,添加您获得的输出和预期的输出。对我来说,它会返回所有记录。它不应该只是返回{“id”:“11”,“name”:“外汇援助”},@user1384058 perfect,看看吧。你能勾选我的答案吗?@user1384058完美,看一看。你能勾选我的答案吗?@user1384058完美,看一看。你能勾选我的答案吗?@user1384058完美,看一看。你能勾选我的答案吗?谢谢NoOutlet的详细解释。这个解决方案也有效。如果我们必须在上面的查询中使用LIKE子句呢?如果我使用下面的查询,它只返回一条记录。db.mgh_facilities.find({“locations.name”:/*N.*/},{“locations.$”:true})是的,返回“仅与查询文档匹配的第一个元素”。如果必须返回多个匹配的子文档,我相信您需要使用聚合管道。类似于:
db.mgh\u facilities.aggregate([{$unwind:$locations},{$match:{“locations.name:/*N.*/},{$group:{{u id:$\u id],responseCode:{$first:$responseCode},locations:{$push:$locations'}}])
应该会给您预期的结果。同样感谢NoOutlet的详细解释。这个解决方案也有效。如果我们必须在上面的查询中使用LIKE子句呢?如果我使用下面的查询,它只返回一条记录。db.mgh_facilities.find({“locations.name”:/*N.*/},{“locations.$”:true})是的,返回“仅与查询文档匹配的第一个元素”。如果必须返回多个匹配的子文档,我相信您需要使用聚合管道。类似于:db.mgh\u facilities.aggregate([{$unwind:$locations},{$match:{“locations.name:/*N.*/},{$group:{{u id:$\u id],responseCode:{$first:$responseCode},locations:{$push:$locations'}}])
应该会给您预期的结果。同样感谢NoOutlet的详细解释。这个解决方案也有效。如果我们必须在上面的查询中使用LIKE子句呢?如果我使用下面的查询,它只返回一条记录。db.mgh_facilities.find({“locations.name”:/*N.*/},{“locations.$”:true})是的,返回“仅与查询文档匹配的第一个元素”。如果必须返回多个匹配的子文档,我相信您需要使用聚合管道。类似于:db.mgh\u facilities.aggregate([{$unwind:$locations},{$match:{“locations.name:/*N.*/},{$group:{{u id:$\u id],responseCode:{$first:$responseCode},locations:{$push:$locations'}}])
应该会给您预期的结果。同样感谢NoOutlet的详细解释。这个解决方案也有效。如果我们必须在上面的查询中使用LIKE子句呢?如果我使用下面的查询,它只返回一条记录。db.mgh_facilities.find({“locations.name”:/*N.*/},{“locations.$”:true})是的,返回“仅与查询文档匹配的第一个元素”。如果必须返回多个匹配的子文档,则