Table of Contents
数据库基本命令操作
数据库常用命令
Collection聚集集合
用户相关
错误信息操作
查看聚集集合基本信息
索引操作
查询操作
1.查询所有
2.显示指定列
3.使用and操作
4.使用or操作
5.使用<, <=, >, >= ($lt, $lte, $gt, $gte )操作,取模运算$mod
6.使用in, not in ($in, $nin)
7.匹配null操作
8.使用like (mongoDB 支持正则表达式)
9.使用distinct、count查询
10.数组查询 (mongoDB自己特有的)(all,size)
11.exists判断是否存在,type判断类型,Sort排序
12.$elemMatch数组元素匹配
更新操作
1.update( criteria, objNew, upsert, multi)、save() 方法
save()方法相当于upsert与multi 都为true时候
2.$inc 对于数字字段的值增加value
3.$set 相当于sql的set field = value
4.$unset 删除字段
5.$push 数组下操作
6.$pushAll 数组下操作
7.$addToSet 数组操作
8.$pop 删除数组内的一个值
9.$pull 数组field内删除一个等于value值
10.$pullAll 数组field内删除多个值
Home Database Mysql Tutorial Mongodb数据库命令端常用操作

Mongodb数据库命令端常用操作

Jun 07, 2016 pm 03:56 PM
mongodb Order Basic Commonly used operate database

数据库基本命令操作 数据库常用命令 1、Help查看命令提示 help db.help(); db.yourColl.help(); db.youColl.find().help(); rs.help(); 2、切换/创建数据库 use yourDB; 当创建一个集合(table)的时候会自动创建当前数据库 3、查询所有数据库 show dbs; 4、删

数据库基本命令操作

数据库常用命令

1、Help查看命令提示

help

db.help();

db.yourColl.help();

db.youColl.find().help();

rs.help();

2、切换/创建数据库

use yourDB; 当创建一个集合(table)的时候会自动创建当前数据库

3、查询所有数据库

show dbs;

4、删除当前使用数据库

db.dropDatabase();

5、从指定主机上克隆数据库

db.cloneDatabase(“127.0.0.1”); 将指定机器上的数据库的数据克隆到当前数据库

6、从指定的机器上复制指定数据库数据到某个数据库

db.copyDatabase("mydb", "temp", "127.0.0.1");将本机的mydb的数据复制到temp数据库中

7、修复当前数据库

db.repairDatabase();

8、查看当前使用的数据库

db.getName();

db; db和getName方法是一样的效果,都可以查询当前使用的数据库

9、显示当前db状态

db.stats();

10、当前db版本

db.version();

11、查看当前db的链接机器地址

db.getMongo();

Collection聚集集合

1、创建一个聚集集合(table)

db.createCollection(“collName”, {size: 20, capped: 5, max: 100});

2、得到指定名称的聚集集合(table)

db.getCollection("account");

3、得到当前db的所有聚集集合

db.getCollectionNames();

4、显示当前db所有聚集索引的状态

db.printCollectionStats();

用户相关

1、添加一个用户

db.addUser("name");

db.addUser("userName", "pwd123", true); 添加用户、设置密码、是否只读

2、数据库认证、安全模式

db.auth("userName", "123123");

3、显示当前所有用户

show users;

4、删除用户

db.removeUser("userName");

错误信息操作

1、查询之前的错误信息 db.getPrevError(); 2、清除错误记录 db.resetError();

查看聚集集合基本信息

1、查看帮助  db.yourColl.help();
2、查询当前集合的数据条数  db.yourColl.count();
3、查看数据空间大小 db.userInfo.dataSize();
4、得到当前聚集集合所在的db db.userInfo.getDB();
5、得到当前聚集的状态 db.userInfo.stats();
6、得到聚集集合总大小 db.userInfo.totalSize();
7、聚集集合储存空间大小 db.userInfo.storageSize();
8、Shard版本信息  db.userInfo.getShardVersion()
9、聚集集合重命名 db.userInfo.renameCollection("users"); 将userInfo重命名为users
10、删除当前聚集集合 db.userInfo.drop();
Copy after login

索引操作

1、创建索引
db.userInfo.ensureIndex({name: 1});
db.userInfo.ensureIndex({name: 1, ts: -1});
 
2、查询当前聚集集合所有索引
db.userInfo.getIndexes();
 
3、查看总索引记录大小
db.userInfo.totalIndexSize();
 
4、读取当前集合的所有index信息
db.users.reIndex();
 
5、删除指定索引
db.users.dropIndex("name_1");
 
6、删除所有索引索引
db.users.dropIndexes();
Copy after login

查询操作

Mongodb-SpringMvc下Query数据库操作SQL
http://blog.csdn.net/xiaohulunb/article/details/27828381

1.查询所有

> db.foo.find()
{ "_id" : ObjectId("5389aa1df06b88aaa313746a"), "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "_id" : ObjectId("5389aaa4afce65313a5614f7"), "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "_id" : ObjectId("5389aabaafce65313a5614f8"), "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "_id" : ObjectId("5389aac5afce65313a5614f9"), "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
Copy after login

2.显示指定列

第一个{} 放where条件 第二个{} 指定哪些列显示和不显示 (0表示不显示 >0表示显示)

后面演示使用{'_id':0} 默认隐藏‘_id列’减少显示量

> db.foo.find({},{&#39;_id&#39;:0,&#39;name&#39;:1,&#39;user&#39;:1})
{ "name" : "yiwa", "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "user" : { "phone" : [ 63, 137 ] } }
Copy after login

3.使用and操作

#名字是yiwa且年龄是25岁

> db.foo.find({&#39;name&#39;:&#39;yiwa&#39;,&#39;age&#39;:25},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
Copy after login

4.使用or操作

#名字是yiwa或者年龄是75岁

> db.foo.find({&#39;$or&#39;:[{&#39;name&#39;:&#39;yiwa&#39;},{&#39;age&#39;:75}]},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
Copy after login

5.使用<, <=, >, >= ($lt, $lte, $gt, $gte )操作,取模运算$mod

#年龄在 15<= x <=75 岁

> db.foo.find({&#39;age&#39;:{&#39;$gte&#39;:15,&#39;$lte&#39;:75}},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
Copy after login

# 对age%3==1的取模结果

> db.foo.find({&#39;age&#39;:{&#39;$mod&#39;:[3,1]}},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
Copy after login

6.使用in, not in ($in, $nin)

#名字不是siwa且年龄在[15,25,85]

> db.foo.find({&#39;name&#39;:{&#39;$nin&#39;:[&#39;siwa&#39;]},&#39;age&#39;:{&#39;$in&#39;:[15,25,85]}},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
Copy after login

7.匹配null操作

#名字是null的

> db.foo.find({&#39;name&#39;:null},{&#39;_id&#39;:0})
> 
Copy after login

8.使用like (mongoDB 支持正则表达式)

#名字like%iwa%的 #名字like yi%的

> db.foo.find({&#39;name&#39;:/iwa/},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.find({&#39;name&#39;:/^yi/},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
Copy after login

9.使用distinct、count查询

> db.foo.distinct(&#39;name&#39;)
[ "yiwa", "erwa", "sanwa", "siwa" ]
> db.foo.count()
4
Copy after login

#distinct结合条件,排序使用

> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 95, "user" : { "phone" : [ 123, 133, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 133, 137, 186 ] } }
> db.foo.distinct("age",{&#39;user.phone&#39;:{&#39;$in&#39;:[63,65,186]}}).sort({&#39;age&#39;:1})
[ 25, 85, 95 ]
> db.foo.distinct("age",{&#39;user.phone&#39;:{&#39;$in&#39;:[63,65,186]}}).sort({&#39;age&#39;:-1})
[ 25, 85, 95 ]
> db.foo.distinct("age",{&#39;user.phone&#39;:{&#39;$in&#39;:[63,65,186]}})
[ 25, 95, 85 ]
Copy after login

待解疑问:?为什么 排序时候 age :-1 与 age :1 结果一样?

10.数组查询 (mongoDB自己特有的)(all,size)

#电话中含有186的

> db.foo.find({&#39;user.phone&#39;:186},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
Copy after login

#电话中含有188,186的

> db.foo.find({&#39;user.phone&#39;:{&#39;$all&#39;:[188,186]}},{&#39;_id&#39;:0})
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
Copy after login

#电话中有2个值的

> db.foo.find({&#39;user.phone&#39;:{&#39;$size&#39;:2}},{&#39;_id&#39;:0})
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
Copy after login

11.exists判断是否存在,type判断类型,Sort排序

#name中值是字符型,age中值是整型,按name升序,age降序

> db.foo.find({&#39;name&#39;:{&#39;$type&#39;:2},&#39;age&#39;:{&#39;$type&#39;:16}},{&#39;_id&#39;:0}).sort({&#39;name&#39;:1,&#39;age&#39;:-1})
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
Copy after login

#name中值存在的:true #name中值不存在的:false

> db.foo.find({&#39;name&#39;:{&#39;$exists&#39;:true}},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.find({&#39;name&#39;:{&#39;$exists&#39;:false}},{&#39;_id&#39;:0})
> 
Copy after login

12.$elemMatch数组元素匹配

#插入测试数据

> db.foo.save({x:[{&#39;a&#39;:1,&#39;b&#39;:5},999,&#39;liw&#39;,{&#39;a&#39;:12},{&#39;b&#39;:100}]})
WriteResult({ "nInserted" : 1 })
Copy after login

#查询某元素中a=1,b=5的元素

> db.foo.find({&#39;x&#39;:{&#39;$elemMatch&#39;:{&#39;a&#39;:1,b:{&#39;$gt&#39;:4}}}},{&#39;_id&#39;:0})
{ "x" : [ { "a" : 1, "b" : 5 }, 999, "liw", { "a" : 12 }, { "b" : 100 } ] }
> db.foo.find({&#39;x.a&#39;:1,&#39;x.b&#39;:5},{&#39;_id&#39;:0})
{ "x" : [ { "a" : 1, "b" : 5 }, 999, "liw", { "a" : 12 }, { "b" : 100 } ] }
Copy after login

更新操作

1.update( criteria, objNew, upsert, multi)、save() 方法

criteria : update的查询条件,类似sql update查询内where后面的
objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

save()方法相当于upsert与multi 都为true时候

> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 55, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({&#39;age&#39;:{$gte:30}},{$set:{&#39;age&#39;:55}},fasle,false)
2014-05-31T19:36:05.407+0800 ReferenceError: fasle is not defined
> db.foo.update({&#39;age&#39;:{$gte:30}},{$set:{&#39;age&#39;:55}},false,false)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 55, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({&#39;age&#39;:{$gte:30}},{$set:{&#39;age&#39;:56}},false,false)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({&#39;age&#39;:{$gte:300}},{$set:{&#39;age&#39;:56}},true,false)
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("5389bee8afce65313a5614fa")
})
> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
{ "age" : 56 }
> db.foo.update({&#39;age&#39;:{$gte:30}},{$set:{&#39;age&#39;:56}},true,true)
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 2 })
> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
{ "age" : 56 }
Copy after login

2.$inc 对于数字字段的值增加value

#年龄大于30的 全部age值增加20

> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 58, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({&#39;age&#39;:{$gte:30}},{$inc:{&#39;age&#39;:20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
Copy after login

3.$set 相当于sql的set field = value

#年龄=56的,设置为名字='laoda',年龄=65

> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({&#39;age&#39;:56},{$set:{&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "laoda", "age" : 65, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
Copy after login

#只更新了一条数据,因为 multi 默认为false

4.$unset 删除字段

#查询name='laoda',user字段存在的数据中,删除age=65的age字段

> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "laoda", "age" : 65, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
{ "age" : 65, "name" : "laoda" }
> db.foo.update({&#39;name&#39;:&#39;laoda&#39;,&#39;user&#39;:{$exists:true}},{$unset:{"age":65}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find({},{&#39;_id&#39;:0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "laoda", "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
{ "age" : 65, "name" : "laoda" }
Copy after login

5.$push 数组下操作

#把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去

> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda" }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$push:{"phone":65}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 65 ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$push:{"phone":[65,75,{&#39;iphone&#39;:&#39;188&#39;},85]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 65, [ 65, 75, { "iphone" : "188" }, 85 ] ] }
Copy after login

6.$pushAll 数组下操作

#一次可以追加多个值到数组

> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda" }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$pushAll:{"phone":[111,222]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222 ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$pushAll:{"phone":[111,222]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222 ] }
Copy after login

7.$addToSet 数组操作

#增加一个值到数组内,而且只有当这个值不在数组内才增加 #插入2次发现,此值存在的时候不插入

> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] }
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$addToSet:{"phone":333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$addToSet:{"phone":333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }
Copy after login

8.$pop 删除数组内的一个值

#删除最后一个值:{ $pop : { field : 1 } }删除第一个值:{ $pop : { field : -1 } }
注意,只能删除一个值,也就是说只能用1或-1,而不能用2或-2来删除两条

> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$pop:{"phone":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$pop:{"phone":-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 222, 111, 222, [ 111, 222 ] ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$pop:{"phone":2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 222, 111, 222 ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$pop:{"phone":333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 222, 111 ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$pop:{"phone":-333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111 ] }
Copy after login

#测试发现,只要是正整数从最后删除,负数从头部删除。

9.$pull 数组field内删除一个等于value值

> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 333 ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$pull:{"phone":333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111 ] }
Copy after login

10.$pullAll 数组field内删除多个值

> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 333, 222 ] }
> db.array.update({&#39;name&#39;:&#39;laoda&#39;,&#39;age&#39;:65},{$pullAll:{"phone":[111,222]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{&#39;_id&#39;:0})
{ "age" : 65, "name" : "laoda", "phone" : [ 333 ] }
Copy after login
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

How to choose a database for GitLab on CentOS How to choose a database for GitLab on CentOS Apr 14, 2025 pm 04:48 PM

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

Real-World MySQL: Examples and Use Cases Real-World MySQL: Examples and Use Cases Apr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

How to set up users in mongodb How to set up users in mongodb Apr 12, 2025 am 08:51 AM

To set up a MongoDB user, follow these steps: 1. Connect to the server and create an administrator user. 2. Create a database to grant users access. 3. Use the createUser command to create a user and specify their role and database access rights. 4. Use the getUsers command to check the created user. 5. Optionally set other permissions or grant users permissions to a specific collection.

See all articles