我是NoSQL数据库的新手,并使用Mongo版本2.4.12。我有一个名为“user”的集合,我想添加一个名为“useremail”的新字段。能告诉我如何从命令行/ shell终端进行操作?
> db.user.find({username :"John"}).pretty();
{
"_id" : "ajd233d-u980-4000-92b6-5353e9602502",
"username" : "John",
"password" : "John",
"firstname" :"John",
"lastname" : "Rogers",
"enabled" : true,
"employeeauth" : [
{
"employeeId" : "a2fg190-b50d-14k2-aan0-ebb7298fa2b7",
"authorities" : [
"DEVELOPER", "TESTER", "CONSULTANT"
]
}
],
}
我想添加新字段“useremail”,因此结果显示如下:
> db.user.find({username :"John"}).pretty();
{
"_id" : "ajd233d-u980-4000-92b6-5353e9602502",
"username" : "John",
"password" : "John",
"firstname" :"John",
"lastname" : "Rogers",
"useremail": "john.rogers@test.com",
"enabled" : true,
"employeeauth" : [
{
"employeeId" : "a2fg190-b50d-14k2-aan0-ebb7298fa2b7",
"authorities" : [
"DEVELOPER", "TESTER", "CONSULTANT"
]
}
],
}
答案 0 :(得分:0)
这里你去db.user.update({username:"John"},{$set:{"useremail":"john.rogers@test.com"}})
{username:"John"}
是查询和
{"useremail":"john.rogers@test.com"}
是更新。
如果文档中不存在字段useremail
,则会插入该字段。如果它已经存在,则该值将被替换。