Home > Database > Mysql Tutorial > mongodb find或者update要用到self或者this的功能

mongodb find或者update要用到self或者this的功能

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:22:02
Original
1130 people have browsed it

mongodb find或者update要用到self或者this的功能 比如你 有 一条 记录是 {name:a,age:1} 在 db.user 里 你想给user 里的 所有用户都增加一个字段 last_name 然后用 name 的内容 变成 {name:a,age:1,last_name:a} 这时候你猜是否可以这么写 db.user.update({

mongodb find或者update要用到self或者this的功能

 

比如你 有 一条 记录是 {‘name’:'a','age':1} 在  db.user 里

 

你想给user 里的 所有用户都增加一个字段 'last_name' 然后用 name 的内容 变成

{‘name’:'a','age':1,'last_name':‘a’}
Copy after login

这时候你猜是否可以这么写

db.user.update({},{$set:{last_name:this.name}})
Copy after login

当然你这么写会报错

网上搜了下 只能这么改

 db.user.find().forEach(function (doc) {
           doc.last_name = doc.name;
           db.user.save(doc); 
    })
Copy after login

 


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
mongodb start
From 1970-01-01 08:00:00
0
0
0
linux - ubuntu14 error installing mongodb
From 1970-01-01 08:00:00
0
0
0
Use of symfony2 mongodb
From 1970-01-01 08:00:00
0
0
0
mongodb _id rename
From 1970-01-01 08:00:00
0
0
0
Parameter understanding of mongodb
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template