【问题标题】:MongoDB. Find all document where element array have on of the needed elementMongoDB。查找元素数组具有所需元素的所有文档
【发布时间】:2014-02-20 23:51:58
【问题描述】:

我有 mongodb 文档

{
  "_id" : ObjectId("4e8ae86d08101908e1000001"),
  "name" : ["Some Name","Another"],
  "zipcode" : ["2223"]
}
{
  "_id" : ObjectId("4e8ae86d08101908e1000002"),
  "name" : ["Another", "Name"],
  "zipcode" : ["2224"]
}
{
  "_id" : ObjectId("4e8ae86d08101908e1000003"),
  "name" : ["Yet", "Another", "Name"],
  "zipcode" : ["2225"]
}

我需要找到“名称数组”具有“另一个”、“名称”值的元素。 我尝试使用 $in,但如果有更多的值,比如 in

{
  "_id" : ObjectId("4e8ae86d08101908e1000003"),
  "name" : ["Yet", "Another", "Name"],
  "zipcode" : ["2225"]
}

它不返回它=(

【问题讨论】:

  • 您能展示一下您使用的$in 语法吗?鉴于答案中的 cmets,尚不清楚您尝试了什么以及您真正期望什么。
  • 我们还可以补充一点,在清理这个问题时,数组元素中“Another”这个词的末尾和周围有个不同的空格。如果这是您的数据实际的样子,那么没有一个值是相等的,因为它们都有不同的空格排列。

标签: mongodb mongodb-query


【解决方案1】:

只要您的数据正确且值包含任何空格,就可以使用 $in(如下所述)。但如果它们不完全相同,您将需要进行一些 $regex 匹配,同时使用 $and 表单:

db.collection.find({
    $and: [
       {name: {$regex: 'Another'} },
       {name: {$regex: 'Name'}} 
    ]
})

如果您只想获取一个文档,该文档同时包含 并且您想要的字段值,并且以相同的顺序只需提供参数作为数组:

db.collection.find({ name: ["Another", "Name" ] })

$in 的用法是匹配任何您作为列表提供的元素。在这种情况下,您将匹配所有文档

db.collection.find({ name: {$in: [ "Another", "Name" ] } })

对于$all 运算符,它的功能是匹配参数列表中包含的所有 元素,因此如果要搜索的数组中不存在参数,则不会包含该参数。所以只有最后两个会匹配:

db.collection.find({ name: {$all: [ "Another", "Name" ] } })


{
  "_id" : ObjectId("4e8ae86d08101908e1000002"),
  "name" : ["Another", "Name"],
  "zipcode" : ["2224"]
}
{
  "_id" : ObjectId("4e8ae86d08101908e1000003"),
  "name" : ["Yet", "Another", "Name"],
  "zipcode" : ["2225"]
}

最后,如果您不知道要匹配的元素的顺序,虽然有点做作,aggregate 为您提供了一种越狱

db.collection.aggregate([
    // Match using $all to reduce the list
    {$match: {name: {$all: ["Name","Another"] }}},

    // Keep the original document in the _id
    {$project:{ 
        _id: {
            _id: "$_id",
            name: "$name",
            zipcode: "$zipcode"
        },
        name: 1
    }},

    // Unwind the "name" array
    {$unwind: "$name"},


    // Count up the entries per _id
    {$group: { _id: "$_id", count: {$sum: 1}}},

    // Only match *2* as there were two elements we were expecting
    {$match: {count: 2} },

    // Project back to the original form
    {$project: { 
        _id:0,
        _id: "$_id._id",
         name: "$_id.name",
         zipcode: "$_id.zipcode"
     }} 
])

这就是我能想到的所有形式。

【讨论】:

    【解决方案2】:

    您是否查看过 $in 的 MongoDB 文档? 找到它here

    您需要这样做:

    find({name: { $in: [ "Another", "Name"] }})
    

    【讨论】:

    • 问题说他们正在使用 $in。自己尝试一下,您不会得到only 问题要求的单个文档。然后自己阅读文档并了解原因。
    • 他要什么单一的文件?正如我从问题中了解到的那样,他正在寻找在数组中同时具有另一个和名称值的任何文档。 find({name: { $in: [ "Another", "Name"] }}) 将返回第二个和第三个文档,无论数组中值的顺序如何。我认为他所做的尝试是您给出的第一个示例,这就是为什么它与他的第三个文档不匹配。这至少是我从问题中所理解的。
    • 我同意叶夫的观点。 @NeilLunn - 我认为需要对这个问题进行更多澄清,并且问题的标题可能应该是“一个所需元素”
    • @WiredPrairie 可能不清楚,是的。英语不热。可能是一个,可能是很多,可能数据是错误的,这正在影响结果。但总的来说,只是试图列出所有排列,以便在问题变成一系列 cmets 之前有一些东西可以查看,例如“我尝试了你所说的但没有用”
    猜你喜欢
    • 2021-03-16
    • 1970-01-01
    • 2014-12-11
    • 2017-09-21
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多