如何通过mongoose更新嵌套的MongoDB对象中的数据?

时间:2017-01-05 16:27:52

标签: mongodb mongoose

我的MongoDB数据库中有这个用户模型模式:

{
    "fullname":  "my name",
     "topics":  [
        {
            "name": "city",         
            "data": null,
            "subset":[ 
                {
                    "name": "tempreture",
                    "data": null,
                    "subset": null
                },
                {
                    "name": "contamination",
                    "data": null,
                    "subset": null
               }
           ]
        }]
    }

我需要mongoose的解决方案来更新数据,其中名称:“污染”来自这样的网址: myname / city / contamination

编辑: 这是我的用户模型(user.js):

  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  var userSchema = Schema({
  fullname: String,
    email: String,
    username: String,
    password: String,
    active: Boolean,
    activator: String,
    topics: [{ type: Object, ref: 'Topic' }],
    clients: [{ type: Object, ref: 'Client' }]
  });

  var topicSchema = Schema({
    name   : String,
    data     : Array,
    subset : [{ type: Object, ref: 'Topic' }]
  });

  var clientSchema = Schema({
    name       : String,
    password   : String,
    permissions : [{ type: Object, ref: 'Permission' }]
  });

  var permissionSchema = Schema({
    topic : String,
    read  : Boolean,
    write : Boolean
  });

  var Permission  = mongoose.model('Permission', permissionSchema);
  var Client  = mongoose.model('Client', clientSchema);
  var Topic  = mongoose.model('Topic', topicSchema);

  module.exports = mongoose.model('User', userSchema);

0 个答案:

没有答案