[英]MongoDB insert and update Field
在我的MongoDB集合中,我有一些字段,其中之一是“ EntitySet”,現在我需要在其中插入一個新字段,例如“ EntityAlchemy”。
現在是這樣的:
"EntitySet": [
{
"Name" : "maka",
"EntityType" : "Person",
"Relevance" : 0.0,
"SentimentScore" : 0.0,
"CountInText" : 0
}
]
更新后應該看起來像這樣
"EntitySet" : [
{
"Name" : "maka",
"EntityType" : "Person",
"Relevance" : 0.0,
"SentimentScore" : 0.0,
"CountInText" : 0
}
],
"EntityAlchemy" : [
{
"Name" : "DZ Bank",
"EntityType" : "Company",
"Relevance" : 0.0,
"SentimentScore" : 0.0,
"CountInText" : 0
}
]
我只能找到如何更新現有字段。 有人可以幫忙嗎?
使用$ set添加更多的鍵值對。
語法是:
db.collection.update({query}, {$set: {key-value pairs}});
例:
db.coll1.update({_id:1},
{$set: "EntityAlchemy" : [
{
"Name" : "DZ Bank",
"EntityType" : "Company",
"Relevance" : 0.0,
"SentimentScore" : 0.0,
"CountInText" : 0
}
]});
您可以將$ push用於數組,如果不存在,mongo會將字段創建為數組。
var alchemy = {
"Name" : "DZ Bank",
"EntityType" : "Company",
"Relevance" : 0.0,
"SentimentScore" : 0.0,
"CountInText" : 0
};
db.getCollection('test').update({_id:1},{$push : {'EntityAlchemy' : alchemy}});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.