1. 程式人生 > 資料庫 >4,mongodb的入門命令

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" }

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 表示只查這一列。

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屬性

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屬性的文件將會被查出

16,