【问题标题】:Mongoose find returns document instead of specific object in arrayMongoose find 返回文档而不是数组中的特定对象
【发布时间】:2017-04-17 22:30:15
【问题描述】:

当我尝试使用 find({query}) 在数组中查找特定对象时,我总是从数组中获取所有元素。 活动数组存储活动(可能有数千个),您可以在以下 sn-p 中看到:

这是我的收藏:

{
    "_id" : ObjectId("58407140755324d04db2ce95"),
    "owner" : 103429326776572,
    "activities" : [
            {
                    "name" : "test1",
                    "startTime" : ISODate("2016-08-11T17:41:54Z"),
                    "type" : "te1",
                    "lat" : 1,
                    "lon" : 1,
                    "creator" : 126212904493088,
                    "coverPhoto" : {
                            "name" : "test1",
                            "path" : "c:\\Users\\Francis\\Desktop\\dusk\\public\\coverPhotos\\SJ9tpP6Mx.jpg"
                    },
                    "identifier" : "H1g9F6vpGl",
                    "users" : [
                            1,
                            2,
                            3
                    ],
                    "hashTags" : [
                            "some",
                            "hashtags"
                    ]
            },
            {
                    "name" : "test2",
                    "startTime" : ISODate("2016-08-11T17:41:53Z"),
                    "type" : "te2",
                    "lat" : 1,
                    "lon" : 1,
                    "creator" : 103312904493090,
                    "coverPhoto" : {
                            "name" : "test2",
                            "path" : "c:\\Users\\Francis\\Desktop\\dusk\\public\\coverPhotos\\Hy8qpvafe.jpg"
                    },
                    "identifier" : "rJlU5TvpMx",
                    "users" : [
                            1,
                            2,
                            3
                    ],
                    "hashTags" : [
                            "some",
                            "hashtags"
                    ]
            }
    ]

}

例如,我需要获取具有特定标识符的活动。 我尝试使用以下查询:

1) db.myCollection.find({'activities.identifier' : "rJlU5TvpMx"})

2) db.myCollection.find({'activities' : { $elemMatch : { "identifier" : "rJlU5TvpMx", "creator" : 103312904493090 } })

以及所有带有 '' 或 "" 符号的组合

我在 mongodb docs 中找到了与我相同的文档架构中的上述查询。

你能告诉我我做错了什么吗?

【问题讨论】:

  • 您收到的错误信息是什么?

标签: javascript node.js mongodb


【解决方案1】:

您可以根据需要尝试使用单匹配或多匹配。这利用了 $elemMatch(projection)

db.myCollection.find({"_id" : ObjectId("58407140755324d04db2ce95")},
             {activities: {$elemMatch: { identifier: "rJlU5TvpMx"}}})

db.myCollection.find( {"_id" : ObjectId("58407140755324d04db2ce95")},
             {activities: {$elemMatch: {creator : 103312904493090, identifier: "rJlU5TvpMx" }}})

【讨论】:

  • 非常感谢!这就是我要找的:)
  • 它会减少查询/搜索时间吗?只是好奇!
  • @BigGuy 与什么比较?它更快,因为查找和投影都可以使用索引。
  • @Veeram 就像我的 find 查询一样返回所有文档,包括所有数组/子文档。那么,$elemMatch 是否有助于以某种方式减少查询以找到我真正想要的内容?
  • @BigGuy $elemMatch 是一个搜索运算符,用于查询具有多个查询条件的嵌入式数组。它可以在可用时使用索引。不确定它是如何在幕后实现的,尽管它只查找第一个匹配的文档。
【解决方案2】:

您正在寻找在查询中作为参数传递的投影对象。它允许从您的搜索中返回特定字段,而不是整个文档。 http://mongoosejs.com/docs/api.html#model_Model.find

我还建议在这里查看对这个问题的回复:Mongoose Query: Find an element inside an array,它使用 unwind 运算符输入数组,因为它似乎与您的需求相关。

【讨论】:

    【解决方案3】:

    在您正在搜索的集合中,您只有一个 Document(Object)。如果您将方法 find() 应用于您的集合,并且里面的查询与 activity.identifier 中的值匹配,它将返回唯一的 Document(object)。

    为了更好地理解我在说什么,请查看 mongoose API 文档上的 example 以及查询结果here

    试试看https://docs.mongodb.com/v3.0/reference/operator/projection/elemMatch/#proj._S_elemMatch

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 2019-03-19
      • 2014-03-24
      • 2019-02-19
      • 1970-01-01
      • 2014-10-24
      • 2012-10-04
      相关资源
      最近更新 更多