Mongodb基础操作语句

1、update

格式:

db.collection.update(criteria,objNew,upsert,multi)

参数说明:

criteria:查询条件

objNew:update对象和一些更新操作符

upsert:如果不存在update的记录,是否插入objNew这个新的文档,true为插入,默认为false,不插入。

multi:默认是false,只更新找到的第一条记录。如果为true,把按条件查询出来的记录全部更新。

删除字段

db.collection.update({"order":{$exists:true}},{$unset:{"order":""}},false,true);

重命名字段

db.collection.update({"order":{$exists:true}},{$rename:{"order":""}},false,true);

更新数据

db.collection.update({"order":{$exists:true}},{$set:{"order":"123"}},{multi:true});

删除数组中的最后一条
db.tmp.update({"_id":ObjectId("5b5584cc8dc9619220792143")},{$pop:{"pictures":1}});
删除数组中的开头第一条
db.tmp.update({"_id":ObjectId("5b5584cc8dc9619220792143")},{$pop:{"pictures":-1}});

//表字段类型修改
db.collection.find().forEach(function(item){
    item.old_product_id = item.old_product_id+"";
    item.new_product_id = item.new_product_id+"";
    item.status = NumberInt(item.status)
    db.collection.save(item);
});

2、add

 

3、remove

 

4、查询

db.collection.find({},{"_id":"$ObjectId","product_id":"$product_id"}).limit(10);
再加上.pretty();就是将find的结果格式化显示。
再加上.limit(NUMBER)表示查询指定数量数据。
再加上.skip(Number)表示跳过指定数量数据。
Limit和skip一起使用可是实现区域查询 limit a and b;
.sort({Key:1/-1});排序,1/-1:升序/降序
注意,fields中还可以有一个参数by:1表示展示字段,by:0隐藏不显示字段,其中by的值跟前面要一直,要么全是0,要么全是1。如不不写by参数的话,前面的值用$这样的格式,用了by的话只能用1或0.

//and查询缺省 db.col.find({key1:value1, key2:value2}).pretty()
db.collection.find({{'_id':ObjectId('55348caf498e13d63b54ffee')},{'product_id':'1'}});
//Or查询
db.collection.find(
   {
      $or: [{key1: value1}, {key2:value2}]
   }
).pretty()
db.collection.find({$or:[{'_id':ObjectId('55348caf498e13d63b54ffee')},{'product_id':'1'}]});
//and和or联合使用
db.col.find({key1:value1, key2:value2, $or: [{key1: value1}, {key2:value2}]}).pretty();
db.collection.find({'_id':ObjectId('55348'),$or:[{'_id':ObjectId('55348')},{'product_id':'1'}]});

逻辑条件查询
$gt -------- greater than  >
$gte --------- gt equal  >=
$lt -------- less than  <
$lte --------- lt equal  <=
$ne ----------- not equal  !=
$eq  --------  equal  =
//19<=age<=20
db.collection.find({"age":{"$gte":19,"$lte":20}}).pretty();

字段长度作为条件
db.collection.find({$where:"this.comments.length < 5"},{"comments":"$comments","datetime",}).limit(5);

模糊查询

模糊查询product_id是以res开头的数据
db.collection.find({"product_id":{$regex:/^res/i}})

Res结尾的数据
db.collection.find({"product_id":{$regex:/res$/i}})
以a开头,b结尾的数据
db.collection.find({"product_id":{$regex:/^a.*.b$/i}})

数据中包含00
db.collection.find({"product_id":{$regex:/.*00.*/i}})
或者
db.collection.find({"product_id":{$regex:“00”}})
或者需要区分大小写
db.collection.find({"product_id":{$regex:/00/i}})

4、聚合查询

管道:将输出的结果作为作为下一个命令的参数
$project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
$match:用于过滤数据,只输出符合条件的文档。$match使用MongoDB的标准查询操作。
$limit:用来限制MongoDB聚合管道返回的文档数。
$skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
$unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
$group:将集合中的文档分组,可用于统计结果。
$sort:将输入文档排序后输出。
$geoNear:输出接近某一地理位置的有序文档。
db.tjComment.aggregate([
{$project:{'t_score':1,'t_num':1}},
{$match:{'t_score':{$gte:3,$lte:4}}},
{$skip:1},
{$limit:10}
{$group:{_id:'$t_score',num_tutorial :{$sum:1}}},
{$sort:{_id:1}}
]);
1、    输出只有t_score和t_num的结果
2、    步骤1的结果过滤数据
3、    步骤2结果剔除第一条数据
4、    限制条数
5、    步骤3结果分组聚合
6、    步骤4结果排序

 

去重计数:

db.getCollection('memberOrder').aggregate([
{$group:{"_id":{"datetime":{$substr:["$datetime",0,10]},"tb_biz_order_id":"$tb_biz_order_id"}}},
{$group:{"_id":"$_id.datetime","ncount":{$sum:1}}},
{$sort:{"_id":-1}}
]);

5、索引

db.collection.createIndex({"pictures.datetime":-1});
db.collection.dropIndex("pictures.datetime");

db.collection.getIndexes();//查看索引
db.collection.dropIndexes();//删除索引
db.collection.totalIndexSize();//索引大小

 

6、批量处理

数据批量处理
db.collection.find({"datetime":{$gte:"2018-01-01 00:00:00"}}).forEach(function(item){        

       var product = db.product.findOne({"orderNo":item.order_number});    

       print(item._id+","+product.shopId+","+product.orderType);      

      db.plComment.update({"_id":item._id},{$set:{"shopId":product.shopId,"orderType":product.orderType}},{ upsert:true , multi          :true});

});

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值