【问题标题】:Create query to find an object in array of objects [duplicate]创建查询以在对象数组中查找对象[重复]
【发布时间】:2019-08-23 21:24:41
【问题描述】:

我想创建一个“读取 x 数据”函数来从对象数组“位置”中的对象“用户”获取 x 数据

我已经尝试过从一个用户那里读取所有数据

    read(req, res) {
    const id= req.params.id;
    data.find({id: id}).then((data) => {
        res.send({data});
    })
}

但是我不知道如何用特定的参数做同样的事情

我的架构:

const UserSchema = new Schema({
  users : [
    {
      id : String,
      name : String,
      others : [
        location : { x : [String], y : [String]}
      ]
    }
  ]
})

我只想从像“bob”这样的用户那里接收 x 数据:

完整的数据架构

{
   "id" : 1,
   "name" : "bob",
   "others": [
     "location" : { "x" : ["here" : "london", "somewhere" : "home"], "y" : ["here" : "london", "somewhere" : "home"] }
   ]
}

我想要什么:

"x" : [
  "here" : "london",
   "somewhere" : "home"
]

【问题讨论】:

  • 我重新编辑了我的问题
  • 我已经读过那个 Quastion 但它有所不同,因为我想要一个用户的所有“x”数据

标签: node.js mongodb mongoose mongodb-query


【解决方案1】:

你可以使用猫鼬的select

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 2019-03-08
    • 2020-04-12
    • 2022-01-24
    • 2017-02-02
    • 2023-03-19
    • 2016-04-17
    • 2018-12-18
    • 1970-01-01
    相关资源
    最近更新 更多