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。