【问题标题】:Get value from results which be query from collection in MongoDB从 MongoDB 集合中查询的结果中获取值
【发布时间】:2016-10-06 17:53:52
【问题描述】:

当我从 MongoDB 中的集合中查询并得到结果时:

[ { details: 
     [ { owner: '57f52829bcc705bb1c37d611',
         nameprd: 'fsfsdaf',
         price: 15000000,
         descrice: 'sfsdfsdaf',
         number: 4,
         dateOff: '2016-06-12T17:00:00.000Z',
         _csrf: 'CPlxeLpq-vYfTTWTgSpR6bsyapbDVgDCKzTc',
         image: 'samsung-galaxy-note-7.png',
         createdAt: '2016-10-06T16:43:11.109Z',
         updatedAt: '2016-10-06T16:43:13.061Z',
         id: '57f67f1f7ab99e5824652208' } ],
    name: 'Máy tính bảng',
    _csrf: 'Ze6OhtgL-2hZvG7TuP9NO4fjY90rA7x46bWA',
    createdAt: '2016-10-05T16:19:53.331Z',
    updatedAt: '2016-10-06T16:43:13.021Z',
    id: '57f52829bcc705bb1c37d611' },
  ]

现在,如何获取这个结果中调用细节的值。

谢谢。

【问题讨论】:

  • 请出示查询数据库的代码。
  • 如果该数组中有多个对象,则需要循环遍历它。如果有一个您期望every time 的结果 - 您可以在 mongoose 中使用 .find。它将返回您可以直接访问的对象 - result.details[0].owner; result.name; result.id

标签: node.js mongodb sails.js sails-mongo


【解决方案1】:

您应该使用以下语法添加查询:,{'details':1}

例如:

如果那是原始查询:

db.person.find({'name':'joe'})

比下面的查询只返回查询的详细值:

db.person.find({'name':'joe'},{'details':1})

添加 ,{'details':1} 意味着您只想获取 details 的数据。它用作广泛查询的过滤器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2021-04-24
    • 2013-10-20
    • 1970-01-01
    相关资源
    最近更新 更多