1
我有一個模式:如何添加數據嵌套數組中mongoshell
{
"id": String,
"username": String,
"password": String,
"email": String,
"firstName": String,
"lastName": String,
"system" : {
"item" : {type: Number},
"update_interval" : { type: Number, max: 99999 },
"reading" : [
{
"id" : { type: Number},
"adc1" : { type: Number, max: 4095 },
"adc2" : { type: Number, max: 4095 },
"pc_datestamp" :Date,
}
]
}
現在我想添加值
"reading" : [
{
"id" : { type: Number},
"adc1" : { type: Number, max: 4095 },
"adc2" : { type: Number, max: 4095 },
"pc_datestamp" :Date,
}
]
,但我不知道我錯了,我試圖從mongoshell更新數據,但沒有成功到現在
> db.users.update({"email" : "[email protected]", "system.item": 1, }, {"$push": {"system.$.reading": [{"adc1" : "123", "adc2": "1245", "id":"1" }] } })
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: system.$.reading"
}
> db.users.update({"email" : "[email protected]", "system": {$elemMatch:{ item: 1}} }, {"$push": {"system.$.reading": {"adc1" : "123", "adc2": "1245", "id":"1" } } })
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
我已經設置項的值作爲一個
> db.users.find({"email" : "[email protected]", "system.item": 1} ).pretty()
{
"_id" : ObjectId("56dd88578ff7fbd714091a4a"),
"lastName" : "test",
"firstName" : "test",
"email" : "[email protected]",
"password" : "$2a$10$wY9wr9oreza4fBX7CfXym.rPZUPrcesigYIfWd0zbM4dDjBy6k3vy",
"username" : "test",
"system" : {
"item" : 1,
"reading" : [ ]
},
"__v" : 0
}
我按照
這 Insert data in nested array in mongodb
,還有更多的問題卻找不到什麼是錯。
感謝@chridam,這是非常有益的。我有一個更懷疑我怎麼增加的「ID」的值,因爲我不斷增加值 – gurumonk
'id'是一個字符串,你可以在不增加它使用'$ inc'更新運算符,因爲它只作用於數值。 – chridam
我不明白「id」:{type:Number},在這裏我已經將它定義爲數字,或者它是類似的東西:[數字],有什麼辦法來增加「ID」 – gurumonk