MongDB日常操作

原创
2019/08/30 19:45
阅读数 310

记录下日常的MongoDB操作命令:

//新建库
use iceportal_image

//新建collection
db.createCollection("xxx-acc",{autoIndexId : true}) ;
db.createCollection("xxx-img",{autoIndexId : true}) ;

//插入数据
db.getCollection("xxx-img").insert(
{_id:ObjectId(),
xxId:"xxx21749",
caption:"GT",
nameZh:"大唐",
nameEn:"xs",
category:"xss",
xxxCode:"STSSW",
url:"/xme/bb",
imageName:"xss",
extension:".jpg",
specs:"12x15",
isDefault:"1",
publicId:"64556",
fileHashCode:"32666",
isDeleted:"1",
createTime:ObjectId("5349b4ddd2781d08c09890f4").getTimestamp(),
updateTime:ObjectId("5349b4ddd2781d08c09890f4").getTimestamp()
}
)

db.getCollection("xxx-acc").insert(
{_id:ObjectId(),
accId:"1",
xxxId:"xxx21749",
accType:"GT",
createTime:ObjectId("5349b4ddd2781d08c09890f4").getTimestamp(),
updateTime:ObjectId("5349b4ddd2781d08c09890f4").getTimestamp()
}
)

//查看collection
show collections

//mongo常用查询语句
//查询数据
db.getCollection("xxx-acc").find({})
   .projection({})
   .sort({_id:-1})
   .limit(100)

//mongodb正则查询
db.xxx201907.find({'_id':{$regex:'xxxA22097'}});
db.xxx201907.find({"availDate":{ "$gte":("2019-06-20 0:0:0")}}).count()
db.getCollection("xxx-filter-2019-10").find({
    _id: "xxx_2019-10"
});


//统计一下xxx库有多少bbID,然后将得到的数据全部放入excel中排重
db.xxx201906.distinct( "bbID",{"availDate":{ "$gte":("2019-06-20 0:0:0")}})
db.xxx201907.distinct( "bbID" );
db.xxx201908.distinct( "bbID" );
db.xxx201909.distinct( "bbID" );
db.xxx201910.distinct( "bbID" );
db.xxx201911.distinct( "bbID" );
db.xxx201912.distinct( "bbID" );
db.xxx202001.distinct( "bbID" );
db.xxx202002.distinct( "bbID" );
db.xxx202003.distinct( "bbID" );
db.xxx202004.distinct( "bbID" );
db.xxx202005.distinct( "bbID" );
db.xxx202006.distinct( "bbID" );

//复杂清理脚步编写
db.xxx201908.find({xxxId:"xxx50181",xxxcode:"CQ",xxxrcode:"xxBAWC"}).forEach(function(b){
    var x_id=b._id;
    //print(_id);
    var xxxId=b.xxxId;
    var xxxcode=b.xxxcode;
    var xxxrcode=b.xxxrcode;
    var availDate=b.availDate;
    var x=xxxId+"-"+xxxcode+"-"+xxxrcode+"-"+availDate;
    //print(x);
    if(x_id!=x){
        print(x_id);
        //db.xxx201908.remove({_id:x_id});
    }
});


//删除库
db.dropDatabase()
//删除集合
db.collection.drop()
//删除一条数据
db.collection.remove({'title':'MongoDB 教程'})
//删除 一条数据
db.getCollection("xxx-acc").remove({'_id':ObjectId("5d5763c61e541088a85bbb43")})

 

参考文档:mongo文档https://www.runoob.com/mongodb/mongodb-create-database.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部