【问题标题】:Search in Embedded Documents in MongoDB?在 MongoDB 的嵌入式文档中搜索?
【发布时间】:2021-06-03 05:34:50
【问题描述】:

我有如图所示的文件

[
    {
        "Users": [
                    {
                        "Name": "Kartikey Vaish",
                        "_id": "1",
                    },
                    {
                        "Name": "Witcher Proxima",
                        "_id": "2",
                    }
                 ],
        "_id": "12",
    },
    {
        "Users": [
                    {
                        "Name": "Witcher Proxima",
                        "_id": "2",
                    },
                    {
                        "Name": "Saga",
                        "_id": "4",
                    }
                 ],
        "_id": "13",
    }
]

我想搜索那些用户数组具有特定 ID 的文档

例如如果

ID == 1 // should return

 [
    {
        "Users": [
                       {
                           "Name": "Kartikey Vaish",
                           "_id": "1",
                       },
                       {
                           "Name": "Witcher Proxima",
                           "_id": "2",
                       }
               ],
          "_id": "12",
    }
]


ID == 2 // should return

 [
    {
        "Users": [
                       {
                           "Name": "Kartikey Vaish",
                           "_id": "1",
                       },
                       {
                           "Name": "Witcher Proxima",
                           "_id": "2",
                       }
               ],
          "_id": "12",
    },
    {
        "Users": [
                       {
                           "Name": "Witcher Proxima",
                           "_id": "2",
                       },
                       {
                           "Name": "Saga",
                           "_id": "4",
                       }
               ],
          "_id": "13",
    }
]

ID == 4 // should return

 [
    {
        "Users": [
                       {
                           "Name": "Kartikey Vaish",
                           "_id": "1",
                       },
                       {
                           "Name": "Saga",
                           "_id": "4",
                       }
               ],
          "_id": "13",
    }
]

正如您从上面看到的,我的查询应该只返回那些“用户”数组包含具有给定 ID 的对象的对象。我试过了,但它不起作用。

const chats = await Chats.find({
      Users: { $elemMatch: { _id: "1" } },
    });
// this returns an empty array

const chats = await Chats.find({
  Users: { $elemMatch: { Name: "Kartikey Vaish" } },
});
// this returns 
 [
    {
        "Users": [
                       {
                           "Name": "Kartikey Vaish",
                           "_id": "1",
                       },
                       {
                           "Name": "Witcher Proxima",
                           "_id": "2",
                       }
               ],
          "_id": "12",
    }
]

我在这里做错了什么? 和_id参数有关系吗?

编辑: 我的聊天模式如下所示 -

const Chats = mongoose.model(
  "Chats",
  new mongoose.Schema({
    Users: {
      type: Array,
      required: true,
      default: [],
    },
  })
);

【问题讨论】:

  • 您的查询是正确的。检查您的架构一次。如果可能,请发布 chats 架构。
  • @DheemanthBhat 我在问题中添加了聊天模式
  • 你为什么要搞乱默认的ObjectId,即_id字段?如果要捕获用户 ID,则创建一个 Number 数据类型的新字段 id(或其他)。

标签: arrays mongodb mongoose mongodb-query


【解决方案1】:

如下所示更新您的架构

const Chats = mongoose.model(
  "Chats",
  new mongoose.Schema({
    Users: [{
      _id: {
        type: String,
        required: true, // Include only if needed!
        unique: true    // Include only if needed!
      },
      Name: {
        type: String,
        index: true    // Include only if needed!
      }
    }]
  })
);

如果您没有明确提及_id,MongoDB 将创建_id 字段为ObjectId

【讨论】:

  • 是的,在更改聊天模式后它起作用了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-18
相关资源
最近更新 更多