mongodb使用总结

一、简单的条件查询
db.getCollection('cdt_shop_act_info').find({"rls_status":"1","shop_status":"1","status":"1"})
多个条件用","隔开。相当于mysql的where and查询
二、in 和 nin
	 db.getCollection('cdt_act_info').find({"banks":{$in:["华夏","招商"]}})
  	 db.getCollection('cdt_act_info').find({"banks":{$nin:["华夏","招商"]}})
三、mogodb查询数组元素
       db.blogs.findOne();  
{  
        "_id" : ObjectId("502262ab09248743250688ea"),  
        "content" : ".....",  
        "comment" : [  
                {  
                        "author" : "joe",  
                        "score" : 3,  
                        "comment" : "just so so!"  
                },  
                {  
                       "author" : "jimmy",  
                        "score" : 5,  
                        "comment" : "cool! good!"  
                }  
        ]  
}  
> db.blogs.find({"comment":{"$elemMatch":{"author":"joe", "score":{"$gte":5}}}});  
> db.blogs.find({"comment":{"$elemMatch":{"author":"joe", "score":{"$gte":3}}}});  
{ "_id" : ObjectId("502262ab09248743250688ea"), "content" : ".....", "comment" : [      {       "author" : "joe",       "score" : 3,    "comment" : "just so so!" },  {       "author" : "jimmy",     "score" : 5,    "comment" : "cool! good!" } ] }  
这样做,结果是正确的!利用条件操作符“$elemMatch”可以组合一组条件,并且还能达到的“点表示法”的模糊查询的效果!
四、复杂数组对象的in查询
db.getCollection('cdt_act_info').find({"banks":{"$elemMatch":{"bankName":{$in:["华夏银行","招商银 行"]}}}})
五、大于,小于,大于或等于,小于或等于、不等于
db.getCollection('cdt_shop_act_info').find({"is_hot":{$ne:0}})
$gt:大于
$lt:小于
$gte:大于或等于
$lte:小于或等于
六、MongoDB中的null和不存在  
查询集合cdt_shop_act_info中y的值为null或者不存在
db.cdt_shop_act_info.find( { “y” : null } )
查询集合cdt_shop_act_info中y的值为null,(仅返回y的值为null的数据,不会返回不存在的)
db.cdt_shop_act_info.find( { “y” : { $type : 10 } } )
还有一种写法如下
db.cdt_shop_act_info.find({“y”:{“$in”:[null], “$exists”:true}})
查询集合cdt_shop_act_info中y的值不存在(不会返回y的值为null的数据)
db.cdt_shop_act_info.find( { “y” : { $exists : false } } )
七、基于坐标的聚合管道查询
db.cdt_shop_act_info.aggregate([
   {
     $geoNear: {
        near: { type: "Point", coordinates: [ 121.279015 , 31.385134 ] },
        distanceField: "calculated",
        minDistance: 1182.51889505108,
        maxDistance: 20000,
        query: { "status": "1","shop_status":"1","rls_status":"1" },
        num:5,
        spherical: true
   	  }
        }
    ])
注:{经纬度有范围限制,应当注意,如果超出范围会报错;在建立索引时经纬度超出范围也会报错导致索引无法建立}
八、删除表中重复数据 sql语句:
 var res = db.cdt_shop_act_info.aggregate([
    {
        $group: { _id: {shopid: '$shop_id',actid: '$act_id'},count: {$sum: 1}}
    },
    {
        $match: {count: {$gt: 1}}
    }
])
for(var i in res._batch){
    var shop_id = res._batch[i]._id.shopid;
    var act_id = res._batch[i]._id.actid;
    var count = res._batch[i].count;
    for(var j=0;j<count-1;j++){
            db.cdt_shop_act_info.remove({"shop_id":shop_id,"act_id":act_id},true)
        }
    
   }
九、mongodb增加字段
db.cdt_shop_act_info.update({}, {$set: {act_click_count:0,act_sort:10}}, {multi: 1})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值