// =================== 批量更新,更新数组中对象 ===================
var cursor = db.language.find({});
cursor.forEach(function(x){
var subSet = db.subscription.find({});
subSet.forEach(function(sub){
var title = sub.name + "(" + x.remarkCn + ")";
db.subscription.updateOne({"_id": sub._id},
{
$push: {
"multiLanguage": {
"language": x.name,
"title" : title,
"desc" : sub.description
}
}
}
)
});
});
// =================== 去重求count ===================
db.WpsFile.distinct("wpsUserId", {"authTime":{$gt:ISODate("2018-12-31T16:00:00.887Z"), $lt:ISODate("2019-01-31T16:00:00.887Z")}}).length
// =================== export ===================
mongoexport -h IP:port -u userName -p pwd -d dbName -c collectionName -q '{authStatus:1, authTime:{$gt:ISODate("2019-01-15T16:00:00.887Z"), $lt:ISODate("2019-01-31T16:00:00.887Z")}}' --type=csv -f _id,fileHash,wpsUserId,wpsFileId,securityFileID,archivedFileID,authNo,authTime,fileType,expireAt,authStatus,source -o /home/ubuntu/mongo-shell/wpsFile.csv
mongoexport -h IP:port -u userName -p pwd -d dbname -c collectionName --type=csv -f _id,type,statDate,count,source -o /home/ubuntu/mongo-shell/requestStat.csv
// =================== import ===================
mongoimport -u uName -p pwd -d dbName -c collectionName --file ./language.json
// =================== modify user ===================
db.updateUser("dbUser",
{
roles : [
{ role: "read", db: "testDB" }
],
pwd: "dkdl1yatgE3jiEBg19dk"
}
)
// =================== aggregate 1 ===================
db.spiderGag.aggregate([
{"$project" : {"tag" : 1}},
{"$group" : {"_id" : "$tag", "count": {"$sum":1}}}
])
db.article.aggregate([
{"$match" : {'subscriptionId': {$exists:false}}},
{"$project" : {"from":1}},
{"$group" : {"_id" : "$from", "count" : {$sum : 1}}}
])
// =================== 数组操作 ===================
// 删除最后一个(1为最后一个元素,-1为第一个元素)
db.subscriptionConfig.updateOne({"subscriptionId" : "5c3ea1cf972d6ed8967792fc"}, {$pop:{'spiderTags': 1}})
// 查询数组中特定数量的结果
db.article.find({ 'images' : { $size : 10 }}).limit(10)
// 占位符更新普通数组 (文档结构:{ "_id" : 1, "semester" : 1, "grades" : [ 70, 87, 90 ] })
db.students.updateOne({semester: 1, grades:90},{$set:{"grades.$":95}})
// 占位符更新对象数组 (文档结构:{ "_id" : 7, "grades" : [ { "grade" : 85, "mean" : 90, "std" : 6 } ] })
db.students.updateOne({'_id':7, 'grades.grade':85}, {$set:{'grades.$.std':10}})
// =================== 查询 - 某字段是否存在 ===================
db.users.find({'sex': {$exists: true}})
参考文章: