Go教程

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总用方法总结的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!