4,mongodb的入门命令

1: mongo入门命令

 show dbs  查看当前的数据库

 use databaseName 选库

 show tables/collections 查看当前库下的collection

 

2, 创建库,创建表,再插入数据

> show dbs;

local  0.078GB

> use shop

switched to db shop

> show dbs;

local  0.078GB

> show collections;

> db.user.insert({name:'lili',age:22});

WriteResult({ "nInserted" : 1 })

> db.user.find();

{ "_id" : ObjectId("5aeeb40d1ca0f3732696f5d3"), "name" : "lili", "age" : 22 }

> show collections;

system.indexes

user

 

3,自定义ID 插入数据

> db.user.insert({_id:2,name:'xiaoming',hobby:['football','basketball'],intro:{title:'wenzhang',local:'beijing'}});

WriteResult({ "nInserted" : 1 })

> db.user.find();

{ "_id" : ObjectId("5aeeb40d1ca0f3732696f5d3"), "name" : "lili", "age" : 22 }

{ "_id" : 2, "name" : "xiaoming", "hobby" : [ "football", "basketball" ], "intro" : { "title" : "wenzhang", "local" : "beijing" } }

4,删除文档(即表)

> db.goods.insert({name:'lili'});

WriteResult({ "nInserted" : 1 })

> show collections;

goods

system.indexes

> show dbs;

admin  0.078GB

local  0.078GB

> db

admin

> db.goods.drop();

true

> show collections;

system.indexes

 

5,删除数据库

> show dbs;

admin  0.078GB

local  0.078GB

> db.dropDatabase()

{ "dropped" : "admin", "ok" : 1 }

> db

admin

> show dbs;

local  0.078GB

6,增加单篇文档

> db.stu.insert({sn:'001',name:'xiaoming'});

WriteResult({ "nInserted" : 1 })

> db.stu.find();

{ "_id" : ObjectId("5aeeb6251ca0f3732696f5d4"), "sn" : "001", "name" : "xiaoming" }

7,增加多个文档(没有指定ID,系统会使用默认的随机ID

> db.stu.insert([{_id:3,name:'zhangfei'},{sn:'004',name:'guanyu'},{sn:005,name:'liubei'}]);

BulkWriteResult({

       "writeErrors" : [ ],

       "writeConcernErrors" : [ ],

       "nInserted" : 3,

       "nUpserted" : 0,

       "nMatched" : 0,

       "nModified" : 0,

       "nRemoved" : 0,

       "upserted" : [ ]

})

> db.stu.find();

{ "_id" : ObjectId("5aeeb6251ca0f3732696f5d4"), "sn" : "001", "name" : "xiaoming" }

{ "_id" : 3, "name" : "zhangfei" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d5"), "sn" : "004", "name" : "guanyu" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d6"), "sn" : 5, "name" : "liubei" }

8,删除指定的行,(如果不写条件删除,会删除所有的行)

> db.stu.find();

{ "_id" : ObjectId("5aeeb6251ca0f3732696f5d4"), "sn" : "001", "name" : "xiaoming" }

{ "_id" : 3, "name" : "zhangfei" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d5"), "sn" : "004", "name" : "guanyu" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d6"), "sn" : 5, "name" : "liubei" }

>

>

> db.stu.remove({sn:'001'});

WriteResult({ "nRemoved" : 1 })

> db.stu.find();

{ "_id" : 3, "name" : "zhangfei" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d5"), "sn" : "004", "name" : "guanyu" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d6"), "sn" : 5, "name" : "liubei" }

4,mongodb的入门命令

9,删除stu表中gender属性为m的文档,只删除1.

db.stu.remove({gender:’m’,true});

10,改操作

结果: 文档中的其他列也不见了,改后只有_id和name列了.

即--新文档直接替换了旧文档,而不是修改

> db.stu.find();

{ "_id" : 3, "name" : "zhangfei" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d5"), "sn" : "004", "name" : "guanyu" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d6"), "sn" : 5, "name" : "liubei" }

> db.stu.update({name:'liubei'},{'name':'liubei ta de son'})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.stu.find();

{ "_id" : 3, "name" : "zhangfei" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d5"), "sn" : "004", "name" : "guanyu" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d6"), "name" : "liubei ta de son" }

如果是想修改文档的某列,可以用$set关键字:

> db.stu.find();

{ "_id" : 3, "name" : "zhangfei" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d5"), "sn" : "004", "name" : "guanyu" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d6"), "name" : "liubei ta de son" }

> db.stu.update({sn:'004'},{$set:{name:'QQ'}});

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.stu.find();

{ "_id" : 3, "name" : "zhangfei" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d5"), "sn" : "004", "name" : "QQ" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d6"), "name" : "liubei ta de son" }

11,

修改时的赋值表达式

$set  修改某列的值

$unset 删除某个列

$rename 重命名某个列

$inc 增长某个列

> db.stu.find();

{ "_id" : 3, "name" : "zhangfei" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d5"), "sn" : "004", "name" : "QQ" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d6"), "name" : "liubei ta de son" }

{ "_id" : ObjectId("5aeec01e1ca0f3732696f5d7"), "name" : "wukong", "jinggu" : "true", "sex" : "m", "age" : 500 }

>db.stu.update({name:'wukong'},{$set:{name:'dzsf'},$unset:{jinggu:1},$rename:{sex:'gender'},$inc:{age:1}});

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.stu.find();

{ "_id" : 3, "name" : "zhangfei" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d5"), "sn" : "004", "name" : "QQ" }

{ "_id" : ObjectId("5aeeb7721ca0f3732696f5d6"), "name" : "liubei ta de son" }

{ "_id" : ObjectId("5aeec01e1ca0f3732696f5d7"), "name" : "dzsf", "age" : 501, "gender" : "m" }

12,

db.news.update({age:21},{$set:{age:22}},{multi:true});

multi的意思是把news中所有age=21的文档,都修改(不设置multi默认只会修改一行)

db.news.update({_id:99},{x:123,y:234},{upsert:true});

如果没有_id=99的文档被修改,就直接插入该文档

13,这里的空的{} 表示查询所有的行。gender:1 表示只查这一列。

4,mongodb的入门命令

14,

例1:db.stu.find()

查询所有文档 所有内容

例2: db.stu.find({},{gendre:1})

查询所有文档,的gender属性 (_id属性默认总是查出来)

例3: db.stu.find({},{gender:1, _id:0})

查询所有文档的gender属性,且不查询_id属性

例3: db.stu.find({gender:’male’},{name:1,_id:0});

查询所有gender属性值为male的文档中的name属性

4,mongodb的入门命令

15,基础查询 where的练习:

15-1主键为32的商品

 db.goods.find({goods_id:32});

15-2不属第3栏目的所有商品($ne)

 db.goods.find({cat_id:{$ne:3}},{goods_id:1,cat_id:1,goods_name:1});

15-3本店价格高于3000元的商品{$gt}

 db.goods.find({shop_price:{$gt:3000}},{goods_name:1,shop_price:1});

15-4本店价格低于或等于100元的商品($lte)

 db.goods.find({shop_price:{$lte:100}},{goods_name:1,shop_price:1});

15-5取出第4栏目或第11栏目的商品($in)

 db.goods.find({cat_id:{$in:[4,11]}},{goods_name:1,shop_price:1});

15-6取出100<=价格<=500的商品($and)

db.goods.find({$and:[{price:{$gt:100},{$price:{$lt:500}}}]);

15-7取出不属于第3栏目且不属于第11栏目的商品($and $nin和$nor分别实现)

 db.goods.find({$and:[{cat_id:{$ne:3}},{cat_id:{$ne:11}}]},{goods_name:1,cat_id:1})

 db.goods.find({cat_id:{$nin:[3,11]}},{goods_name:1,cat_id:1});

 db.goods.find({$nor:[{cat_id:3},{cat_id:11}]},{goods_name:1,cat_id:1});

15-8取出价格大于100且小于300,或者大于4000且小于5000的商品()

db.goods.find({$or:[{$and:[{shop_price:{$gt:100}},{shop_price:{$lt:300}}]},{$and:[{shop_price:{$gt:4000}},{shop_price:{$lt:5000}}]}]},{goods_name:1,shop_price:1});

15-9取出goods_id%5 == 1, 即,1,6,11,..这样的商品

db.goods.find({goods_id:{$mod:[5,1]}});

15-10取出有age属性的文档

db.stu.find({age:{$exists:1}});

15-11含有age属性的文档将会被查出

4,mongodb的入门命令

16,

4,mongodb的入门命令

4,mongodb的入门命令

 

上一篇:MongoDB高级查询aggregate聚合管道


下一篇:MongoDB中的_id