简体   繁体   English

MongoDB 中的嵌入式阵列插入

[英]Embedded array insert in MongoDB

For practice purposes, I have 3 users which I have inserted into my database.出于练习目的,我有 3 个用户已插入到我的数据库中。

JSON code: JSON 代码:

[
    {
    "f_name": "Raf", 
    "l_name":"Bak", 
    "age": 19, 
    },
    {
    "f_name":"miltos",
    "l_name":"Bak", 
    "age": 56,
    },
    {
    "f_name":"eleni",
    "l_name":"kot", 
    "age": 27,
    }
]

I want to insert for every user a history array and within that array another array.我想为每个用户插入一个历史数组,并在该数组中插入另一个数组。 I use the following JSON code but I get an error.我使用以下 JSON 代码,但出现错误。

db.patientdata.insertOne({f_name:"Raf"},{$set: {history:["desease":"fever","treatment":"ponstan"]}})

在此处输入图像描述

Do I have to "$set" the 2nd array as well?我是否也必须“$set”第二个数组?

Thank you in advance.先感谢您。

edit after further meddling with this:在进一步干预后进行编辑:

I also tired the following JSON code:我也厌倦了以下 JSON 代码:

db.patientdata.updateOne({f_name:"Raf"},{$set: {"history":["desease","treatment"]}})
db.patientdata.updateOne({f_name:"Raf"},{$set: {"desease":["fever"]}})
db.patientdata.updateOne({f_name:"Raf"},{$set: {"treatment":["ponstan"]}})

but I there wasn't a second embedded array within the first one:但我在第一个数组中没有第二个嵌入式数组: 在此处输入图像描述

I inserted a new user from the beginning and it works fine.我从一开始就插入了一个新用户,它工作正常。

JSON code: JSON 代码:

db.patientdata.insert({f_name:"George",l_name:"Bak","age":29, history:[{disease:"doesn't know good programming",treatment: "study more"}]})

当我搜索结果时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM