MongoDB日常语法笔记01

MongoDB常用语法

mongo 删除语句

db.getCollection("_User").remove({
    '_id': {
        "$in": ["qwewre", "qwerwewqweq", "dsffdasd" ]
    }
})

mongo 分组统计语句

db.getCollection("_User").aggregate([
    {
        $group: {
            _id: {
                $toLower: "$email"
            },
            count: {
                $sum: 1
            }
        }
    },
    {
        $match: {
            count: {
                $gte: 2
            },
                    email: {
                $ne: ""
            },
        }
    },
    {
        $sort: {
            count: - 1
        }
    }
]);

MongoDB 查询忽略大小写语句

db.getCollection("_User").aggregate([
    {
        $project: {
            username: 1,
            email: 1,
            firstName: 1,
            lastName: 1,
            gender: 1,
            phone: 1,
            avatar: 1,
            age: 1,
            birthday: 1,
            emailVerified: 1,
            shopifyUserId: 1,
            registerFrom: 1,
            country: 1,
            countryCode: 1,
            state: 1,
            config: 1,
            subscriptionInfo: 1,
            _hashed_password: 1,
            _auth_data_facebook: 1,
            _email_verify_token: 1,
            _perishable_token: 1,
            tracking_level: 1,
            timezone: 1,
            mileageEnd: 1,
            batteryEnd: 1,
            disturbFreeZoneEnable: 1,
            appConfig: 1,
            ref_id: 1,
            _updated_at: 1,
            _created_at: 1,
            emailTemp: {
                $in: [{
                    $toLower: "$email"
                },["zhong+022002@nonda.us","corehonecut1157@aol.com"]]
            }
        }
    }
    ,
    {
        $match: {
            emailTemp: {
                $eq: true
            },
            
        }
    },
    {
        $sort: {
            _updated_at: - 1
        }
    },
    
])

普通查询 语法

db.getCollection('Vehicle').find({'vin':{$ne:[null],$exists:true}})

分页查询

db.getCollection("ZUSLastParkingHistory").find().limit(1000).skip(0)

条件筛选查询

//大于等于2012.11.7 小于等于2016.11.7
db.getCollection('ZUSDevice').find({
    "bindTime": {
        $gte: new Date(2012, 11, 7)
    },
    "bindTime": {
        $lte: new Date(2016, 11, 7)
    }
})

db.getCollection('ZUSDevice').find({
    "bindTime": {
        $gte: new Date('2016-10-02T00:00:00Z')
    },
    "bindTime": {
        $lte: new Date('2016-10-02T07:58:51Z')
    }
})

筛选统计

db.VoltageFullData.find().count();

去重

 db.ZUSLastParkingHistory.distinct("_p_vehicle",{parkingTime: {  $gte: new Date(2018, 08, 01), $lt: new Date(2018, 09, 01) }}).length

 db.VoltageFullData.distinct("_p_vehicle",{time: {  $gte:"1514736000000" , $lt: "1517414400000" }}).length

db.getCollection(‘VoltageFullData’).find({
“_created_at”: {
$gte: new Date(‘2019-11-01T00:00:00Z’)
},
“_created_at”: {
$lt: new Date(‘2019-12-01T00:00:00Z’)
}
})

批量更新

db.getCollection("ZUSDevice").update({
    "_p_owner": {
        "$in": ["_User$AxcMbt1igO"]
    }
}, {
    $set: {
        'valid': 0
    }
},{multi:true})

如果你要修改多条相同的文档,则需要设置 multi 参数为 true。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做紧咩嘢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值