MongDB日常操作

记录下日常的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

转载于:https://my.oschina.net/u/2371923/blog/3100017

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值