Home > Database > Mysql Tutorial > MongoDB 可读性比较差的语句

MongoDB 可读性比较差的语句

WBOY
Release: 2016-06-07 17:23:38
Original
1012 people have browsed it

学习update语句,和$push修改器,书上举得例子让我百思不得其解,update()的前两个参数分别是:查询,新文档。书上的例子如下所示

今天学习update语句,和$push修改器,书上举得例子让我百思不得其解,update()的前两个参数分别是:查询,新文档。书上的例子如下所示,在集合里面找Key是Gende不是Mr的项,找到之后,把他的Gender设置为M。按照我的想法,谁都没有Gendar属性,,那么条件不满足,后面的新文档就不会生成咯,但是执行的结果却大相径庭,因为所有文档都没有Gender属性,所以第一条文档被返回了,而Gender属性也加到了第一条文档during上!

> db.test.remove() 

> db.test.insert( {"name" : "during"} ) 
> db.test.insert( {"name" : "puncha"} ) 

> db.test.find() 
{ "_id" : ObjectId("50c21581810234135d194bcc"), "name" : "during" } 
{ "_id" : ObjectId("50c21586810234135d194bcd"), "name" : "puncha" } 

> db.test.update( {"Gender" : {"$ne":"M"}},{"$push": {"Gender" : "M"}} ) 

> db.test.find() 
{ "_id" : ObjectId("50c21586810234135d194bcd"), "name" : "puncha" } 
{ "Gender" : [ "M" ], "_id" : ObjectId("50c21581810234135d194bcc"), "name" : "du 
ring" } 

linux

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template