用户工具

站点工具


分享:技术:mongodb:mongodb的介绍

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
分享:技术:mongodb:mongodb的介绍 [2015/07/26 13:53]
gxx
分享:技术:mongodb:mongodb的介绍 [2015/07/26 15:06] (当前版本)
gxx
行 169: 行 169:
 gxx@iZ23goxo66aZ:​~$ ​ gxx@iZ23goxo66aZ:​~$ ​
 </​code>​ </​code>​
 +<​code>​
 +gxx@iZ23goxo66aZ:​~$ mongo #​进入mongodb
 +MongoDB shell version: 2.4.9 #​当前版本
 +connecting to: test #​默认进入test数据库
 +> db.mycol3.find() #​查看集合mycol3所有的文档
 +{ "​_id"​ : ObjectId("​55b471effe2d347c6dd80253"​),​ "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80254"​),​ "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80255"​),​ "​cld"​ : "​曹丽东"​ }
 +{ "​_id"​ : ObjectId("​55b47205fe2d347c6dd80256"​),​ "​name"​ : "​周哲博",​ "​age"​ : 20, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +> db.mycol3.update({"​age":​{$ne:​10}},​{$set:​{"​age":​25}}) #update set age=25 where age!=10,只修改第一个文档,想修改多条,必须带上{multi:​true}
 +> db.mycol3.find() #​查看集合mycol3所有的文档
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80254"​),​ "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80255"​),​ "​cld"​ : "​曹丽东"​ }
 +{ "​_id"​ : ObjectId("​55b47205fe2d347c6dd80256"​),​ "​name"​ : "​周哲博",​ "​age"​ : 20, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +{ "​_id"​ : ObjectId("​55b471effe2d347c6dd80253"​),​ "​age"​ : 25, "​name"​ : "​关向辉"​ }
 +> db.mycol3.update({"​age":​{$ne:​10}},​{$set:​{"​age":​25}},​{multi:​true}) #​带上{multi:​true},修改多个文档
 +> db.mycol3.find() #​查看集合mycol3所有的文档
 +{ "​_id"​ : ObjectId("​55b47205fe2d347c6dd80256"​),​ "​name"​ : "​周哲博",​ "​age"​ : 25, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +{ "​_id"​ : ObjectId("​55b471effe2d347c6dd80253"​),​ "​age"​ : 25, "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80254"​),​ "​age"​ : 25, "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80255"​),​ "​age"​ : 25, "​cld"​ : "​曹丽东"​ }
 +> exit #​退出mongodb
 +bye
 +gxx@iZ23goxo66aZ:​~$ ​
 +</​code>​
 +<​code>​
 +gxx@iZ23goxo66aZ:​~$ mongo #​进入mongodb
 +MongoDB shell version: 2.4.9 #​当前版本
 +connecting to: test #​默认进入test数据库
 +> db.mycol.insert #​查看insert的定义,如果obj._id的文档已存在,插入失败
 +function ( obj , options, _allow_dot ){
 +    if ( ! obj )
 +        throw "no object passed to insert!";​
 +    if ( ! _allow_dot ) {
 +        this._validateForStorage( obj );
 +    }
 +
 +    if ( typeof( options ) == "​undefined"​ ) options = 0;
 +
 +    if ( typeof( obj._id ) == "​undefined"​ && ! Array.isArray( obj ) ){
 +        var tmp = obj; // don't want to modify input
 +        obj = {_id: new ObjectId()};​
 +        for (var key in tmp){
 +            obj[key] = tmp[key];
 +        }
 +    }
 +    var startTime = (typeof(_verboseShell) === '​undefined'​ ||
 +                     ​!_verboseShell) ? 0 : new Date().getTime();​
 +    this._mongo.insert( this._fullName , obj, options );
 +    this._lastID = obj._id;
 +    this._printExtraInfo("​Inserted",​ startTime);
 +}
 +> db.mycol.save #​查看save的定义,可以看到如果obj._id存在,执行update,不存在执行insert
 +function ( obj ){
 +    if ( obj == null || typeof( obj ) == "​undefined"​ )
 +        throw "​can'​t save a null";
 +
 +    if ( typeof( obj ) == "​number"​ || typeof( obj) == "​string"​ )
 +        throw "​can'​t save a number or string"​
 +
 +    if ( typeof( obj._id ) == "​undefined"​ ){
 +        obj._id = new ObjectId();
 +        return this.insert( obj );
 +    }
 +    else {
 +        return this.update( { _id : obj._id } , obj , true );
 +    }
 +}
 +> db.mycol3.find() #​查看集合mycol3的所有文档
 +{ "​_id"​ : ObjectId("​55b47205fe2d347c6dd80256"​),​ "​name"​ : "​周哲博",​ "​age"​ : 25, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +{ "​_id"​ : ObjectId("​55b471effe2d347c6dd80253"​),​ "​age"​ : 25, "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80254"​),​ "​age"​ : 25, "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80255"​),​ "​age"​ : 25, "​cld"​ : "​曹丽东"​ }
 +> db.mycol3.insert({ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80255"​),​ "​age"​ : 30, "​cld"​ : "​曹丽东"​ }) #​_id=55b471f3fe2d347c6dd80255的文档已存在,插入失败
 +E11000 duplicate key error index: test.mycol3.$_id_ ​ dup key: { : ObjectId('​55b471f3fe2d347c6dd80255'​) }
 +> db.mycol3.save({ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80255"​),​ "​age"​ : 30, "​cld"​ : "​曹丽东"​ }) #​_id=55b471f3fe2d347c6dd80255的文档如果不存在,执行insert插入,如果已存在,执行update修改
 +> db.mycol3.find() #​查看集合mycol3的所有文档,_id=55b471f3fe2d347c6dd80255的age已被修改
 +{ "​_id"​ : ObjectId("​55b47205fe2d347c6dd80256"​),​ "​name"​ : "​周哲博",​ "​age"​ : 25, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +{ "​_id"​ : ObjectId("​55b471effe2d347c6dd80253"​),​ "​age"​ : 25, "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80254"​),​ "​age"​ : 25, "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80255"​),​ "​age"​ : 30, "​cld"​ : "​曹丽东"​ }
 +> exit #​退出mongodb
 +bye
 +gxx@iZ23goxo66aZ:​~$ ​
 +</​code>​
 +<​code>​
 +gxx@iZ23goxo66aZ:​~$ mongo #​进入mongodb
 +MongoDB shell version: 2.4.9 #​当前版本
 +connecting to: test #​默认进入test数据库
 +> db.mycol3.find() #​查看集合mycol3的所有文档
 +{ "​_id"​ : ObjectId("​55b47205fe2d347c6dd80256"​),​ "​name"​ : "​周哲博",​ "​age"​ : 25, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +{ "​_id"​ : ObjectId("​55b471effe2d347c6dd80253"​),​ "​age"​ : 25, "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80254"​),​ "​age"​ : 25, "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80255"​),​ "​age"​ : 30, "​cld"​ : "​曹丽东"​ }
 +> db.mycol3.remove({'​age':​30}) #​删除集合mycol3中age=30的文档
 +> db.mycol3.find() #​查看集合mycol3的所有文档
 +{ "​_id"​ : ObjectId("​55b47205fe2d347c6dd80256"​),​ "​name"​ : "​周哲博",​ "​age"​ : 25, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +{ "​_id"​ : ObjectId("​55b471effe2d347c6dd80253"​),​ "​age"​ : 25, "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b471f3fe2d347c6dd80254"​),​ "​age"​ : 25, "​syl"​ : "​沈云龙"​ }
 +> db.mycol3.remove() #​删除集合mycol3所有文档
 +> db.mycol3.find() #​查看集合mycol3的所有文档,已被清空
 +> exit #​退出mongodb
 +bye
 +gxx@iZ23goxo66aZ:​~$ ​
 +</​code>​
 +<​code>​
 +gxx@iZ23goxo66aZ:​~$ mongo #​进入mongodb
 +MongoDB shell version: 2.4.9 #​当前版本
 +connecting to: test #​默认进入test数据库
 +> db.mycol3.insert({"​name":"​关向辉"​}) #​插入文档
 +> db.mycol3.insert([{"​syl":"​沈云龙"​},​{"​cld":"​曹丽东"​}]) #​插入多个文档,使用数组
 +> db.mycol3.insert({"​name":"​周哲博","​age":​20,"​love":​["​apple","​pear"​]}) #​插入文档
 +> db.mycol3.find() #​查看集合mycol3的所有文档
 +{ "​_id"​ : ObjectId("​55b47f3b0a35077a1f579fe0"​),​ "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe1"​),​ "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe2"​),​ "​cld"​ : "​曹丽东"​ }
 +{ "​_id"​ : ObjectId("​55b47f460a35077a1f579fe3"​),​ "​name"​ : "​周哲博",​ "​age"​ : 20, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +> db.mycol3.find({},​{"​_id":​0,"​name":​1,"​cld":​1}) #​查看集合mycol3的所有文档,并投影字段,显示name和cld字段,设置字段_id为0或者不设置字段syl,​age,​love等都为不投影
 +{ "​name"​ : "​关向辉"​ }
 +{  }
 +{ "​cld"​ : "​曹丽东"​ }
 +{ "​name"​ : "​周哲博"​ }
 +> exit #​退出mongodb
 +bye
 +gxx@iZ23goxo66aZ:​~$ ​
 +</​code>​
 +<​code>​
 +gxx@iZ23goxo66aZ:​~$ mongo #​进入mongodb
 +MongoDB shell version: 2.4.9 #​当前版本
 +connecting to: test #​默认进入test数据库
 +> db.mycol3.find({"​age":​{$ne:​20}}) #​查看集合mycol3的age!=20的文档
 +{ "​_id"​ : ObjectId("​55b47f3b0a35077a1f579fe0"​),​ "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe1"​),​ "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe2"​),​ "​cld"​ : "​曹丽东"​ }
 +> db.mycol3.find({"​age":​{$ne:​20}}).limit(2) #​查看集合mycol3的age!=20的文档,只返回2条
 +{ "​_id"​ : ObjectId("​55b47f3b0a35077a1f579fe0"​),​ "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe1"​),​ "​syl"​ : "​沈云龙"​ }
 +> db.mycol3.find({"​age":​{$ne:​20}}).limit(2).skip(1) #​查看集合mycol3的age!=20的文档,过滤第一条,只返回2条
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe1"​),​ "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe2"​),​ "​cld"​ : "​曹丽东"​ }
 +> db.mycol3.find().sort({"​name":​1}) #​查看集合mycol3的所有文档,按name正序排列
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe1"​),​ "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe2"​),​ "​cld"​ : "​曹丽东"​ }
 +{ "​_id"​ : ObjectId("​55b47f3b0a35077a1f579fe0"​),​ "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b47f460a35077a1f579fe3"​),​ "​name"​ : "​周哲博",​ "​age"​ : 20, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +> db.mycol3.find().sort({"​name":​-1}) #​查看集合mycol3的所有文档,按name反序排列
 +{ "​_id"​ : ObjectId("​55b47f460a35077a1f579fe3"​),​ "​name"​ : "​周哲博",​ "​age"​ : 20, "​love"​ : [  "​apple", ​ "​pear"​ ] }
 +{ "​_id"​ : ObjectId("​55b47f3b0a35077a1f579fe0"​),​ "​name"​ : "​关向辉"​ }
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe1"​),​ "​syl"​ : "​沈云龙"​ }
 +{ "​_id"​ : ObjectId("​55b47f420a35077a1f579fe2"​),​ "​cld"​ : "​曹丽东"​ }
 +> exit #​退出mongodb
 +bye
 +gxx@iZ23goxo66aZ:​~$ ​
 +</​code>​
 +<​code>​
 +gxx@iZ23goxo66aZ:​~$ mongo #​进入mongodb
 +MongoDB shell version: 2.4.9 #​当前版本
 +connecting to: test #​默认进入test数据库
 +> db.mycol3.ensureIndex({"​name":​1}) #​创建索引提高查询效率,并设置1正序和-1倒序
 +> db.mycol3.ensureIndex({"​name":​1,"​age":​-1}) #​设置多个字段索引提高查询效率
 +> exit #​退出mongodb
 +bye
 +gxx@iZ23goxo66aZ:​~$ ​
 +</​code>​
 +mongodb暂没操作的部分:
 +  * mongodb的聚合
 +  * mongodb的复制
 +  * mongodb的备份和恢复
分享/技术/mongodb/mongodb的介绍.1437890003.txt.gz · 最后更改: 2015/07/26 13:53 由 gxx