数据库
首页 > 数据库> > mongodb总用方法总结

mongodb总用方法总结

作者:互联网

mongodb总用方法总结

// aggregate
db.lily_k_infomation.aggregate([
    {$project:
        {
                       // 字符串拼接
            _id:{"$toString":"$_id"},
            infoid: "$id",
            keyword: 1,
            
            path: { "$concat" : ["/detail", "$urlpath"]},
            // 修改字段名称, 必须引号包裹
//            aid: "$allianceid",
         }
   },
    //    {$match: {}},

])

// 查询
db.lily_infomation.find({"links": null})

// 修改字段名称
db.getCollection("lily_infomation").update({}, {$rename: {"specialtopics": "topic"}}, {multi: true})
// forEach
db.getCollection("lily_infomation").find({}).forEach(function(item){
    var Id = item._id;
    var infoid = item.infoid;
    print(Id, infoid)
    var li = []
    db.lily_specialtopicinformation_new.find( { InfoId: infoid }).forEach(function(item_label){
            var SpecialTopicId = item_label.SpecialTopicId;
            
            //print(SpecialTopicId);
            
            db.lily_specialtopicalliance.find( { id: SpecialTopicId }).forEach(function(item_label_){
                var li_id = item_label_.id;
                var li_name = item_label_.words;
                //print(li_id, li_name)
               li.push({"id": li_id, "name": li_name, "urlpath": "/peixun/k_" + li_id + ".html" })
            
        
            
        });
            // print(li);
        });
    db.getCollection("lily_infomation").update( { _id: Id }, { $set: { topic: li } });
    });

// 创建索引
db.publish_tp.createIndex({"uuid": 1})

// or
db.syd.find({"$or": [{"lable1": "综合"}, {"lable1": "少儿"}]})

// group
db.syd.aggregate([{$group : {_id : "$lable1", total : {$sum : 1}}}])

db.fanwen_c.aggregate([
        {
            $group : {_id : "$status", num_tutorial : {$sum : 1}}, 
        },
        {
            $sort: {_id: 1}
        }
    ])

// regex
db.publish.find({"title": {"$regex":"_"}, "status": 22})

// $lt  $gt
db.publish_tp.find({"status": 1, "dateCreated": {"$lt": "2021-06-01"}}).sort({"dateCreated": -1}).count()

// count
db.asx.find().count()

// $rename 修改字段名称
db.getCollection("lily_infomation").update({}, {$rename: {"specialtopics(旧字段名)": "topic(新字段名)"}}, {multi: true})

//

// 数组去重 function 
function unique (arr) {
  return Array.from(new Set(arr))
}

db.getCollection("lily_teacher").update( { _id: item._id }, { $set: { labels: unique(new_labelid) } });

 

标签:总结,mongodb,db,li,item,find,总用,lily,id
来源: https://www.cnblogs.com/xiao-xue-di/p/15527451.html