如何在mongoose中查询对象属性(Field)?

时间:2017-04-07 10:45:49

标签: node.js mongodb mongoose mongoose-schema

我想使用 mediaId 获取一个文档,该文档放在mongoose对象字段中

模式

mongoose  = require 'mongoose'
Schema    = mongoose.Schema
Promise   = require "bluebird"

DB_NAME   = 'test'

fields =
  presentationId: String
  customerId: String
  json: Object

collection =
  collection: DB_NAME

schema = new Schema fields, collection

schema.plugin timestamps

model = mongoose.model DB_NAME, schema
Promise.promisifyAll model

module.exports = model
module.exports.col = DB_NAME

DB如下

    {
  "presentationId": "58e38fdbb9d4f20dc5f0bbcd",
  "customerId": "589b2599f304602ff8a17490",
  "json": {
    "objects": [
       {
          "mediaId": "58e34c8454a8d361ea6967e7",
          "duration": 10,
          "name": "image1.png",
          "height": 92,
          "width": 272,
          "top": 12.61,
          "left": 25.26,
          "type": "image"
        },
        {
          "mediaId": "58e34c8454a8d361ea6967e7",
          "duration": 10,
          "name": "image2.png",
          "height": 92,
          "width": 272,
          "top": 12.61,
          "left": 25.26,
          "type": "image"
        },
        {
          "mediaId": "58e34c8454a8d361ea6967e8",
          "duration": 10,
          "name": "image3.png",
          "height": 92,
          "width": 272,
          "top": 12.61,
          "left": 25.26,
          "type": "image"
        }
    ]
  }
}

上面的代码是我的Schema和数据库文档 如果我的mediaId与db mediaId匹配,我想获取文档 的例如:

query = 
  json:
   objects:
    mediaId: "58e34c8454a8d361ea6967e7"

0 个答案:

没有答案