Mongodb 的常用方法,在可视化工具执行

查询:

db.getCollection('message').find({"userId":"31257"})

 查询总数:

db.getCollection('message').find({"sendTimeStamp":{$gt:1517982993}}).count()

 条件删除数据:

db.getCollection('message').remove({'title':'MongoDB 教程'})

 循环插入数据:

for(var i=1;i<10;i++) db.getCollection('message').insert({"userId":"19290","pointsDelta" : 0})

 插入一条记录:

db.getCollection('message').insert({"userId":"19290","pointsDelta" : 0})

 当前数据库状态:

db.getCollection('message').stats()

 查看内存使用大小

show dbs

 profiling功能

db.getProfilingLevel(1)

 0 代表不开启

1 代表记录慢指令(默认100ms)

2 代表记录所有指令

db.setProfilingLevel(1)

 删除查询的数据

show dbs
use cpeducloud

//db.getCollection('historylog').count();

var mydata = db.getCollection('historylog').find({},{'_id':1}).sort({'_id':1}).limit(10);
while(mydata.hasNext()){
    var one = mydata.next();
    printjson(one);
    db.getCollection('historylog').remove(one);
 }

  //db.getCollection('historylog').remove({'_id':ObjectId("5d2304aab8b5ea5745d0b415")});

根据时间查询

 db.getCollection('applog').remove({"startTime":{$lte:ISODate("2019-11-15 02:18:56.248Z")}});

更改此值,使其生效。

原文地址:https://www.cnblogs.com/sunxun/p/9646279.html