[英]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.