查找和更新对象到数组到猫鼬数组

时间:2018-07-03 20:55:05

标签: node.js mongodb mongoose

让我们说我在Mongodb上有这个json:

 {
"_id": {
    "$oid": "5b37f4b5fe0cd65b1739641b"
},
"email": "sample@me.com",
"username": "Sample",
"password": "$2b$10$Zpynx8wTgqQftUO3m1tLUO8lNHtBtxl0i7yk84rRkVzcTmB9zO7Mu",
"__v": 0,
"tds": [
    {
        "_id": {
            "$oid": "5b3bebb247737b77b2ecc398"
        },
        "category": "Projects",
        "todos": {
            "_id": {
                "$oid": "5b3bebb247737b77b2ecc397"
            },
            "todo": "something to do"
        }
    },
    {
        "_id": {
            "$oid": "5b3bec41dc270277bae759d6"
        },
        "category": "Main",
        "todos": {
            "_id": {
                "$oid": "5b3bec41dc270277bae759d5"
            },
            "todo": "Other to do"
        }
    }
]

}

如何更新待办事项? 例如:

  {
        "_id": {
            "$oid": "5b3bebb247737b77b2ecc397"
        },
        "todo": "something to do"
    }

进入

  {
        "_id": {
            "$oid": "5b3bebb247737b77b2ecc397"
        },
        "todo": "Lets do something"
    }

我在nodeJS上尝试使用select,$ push,$ set,但是任何人都可以做到这一点。也许有一个我不知道的过滤器。当我沉迷于此两天后,我决定放弃。

模式:

var UserSchema = new mongoose.Schema({
  email: {
    type: String,
    unique: true,
    required: true,
    trim: true
  },
  username: {
    type: String,
    unique: true,
    required: true,
    trim: true
  },
  password: {
    type: String,
    required: true,
  },
  passwordConf: {
    type: String,
    required: true,
  },
  tds:{}
})

;

编辑:

带有有效ID的新有效json。

1 个答案:

答案 0 :(得分:0)

您可以使用mongoose中的updateOne()和updateMany()函数更新json数据,如下所示:

if(this.props.dataLoaded){
  <loadingComponent></loadingComponent>
} else {
  //operation is performed
}