mongodb常用的sql语句总结

本文为博主原创,转载请注明出处:

1 .  查询所有

db.getCollection('CollectionName').find()

2.根据条件查询

db.getCollection('CollectionName').find({"userId":37761});

3.多条件查询

db.getCollection('CollectionName').find({"userId":1},{"customerId":61});

4.根据时间戳范围查询

db.getCollection('CollectionName').find({"userId":61},{"timestamp":{"$gt":1540449300000,"$lte":1540550100000}})

5.条件查排序并分页:1.是升序,  -1是降序 

db.getCollection('CollectionName').find({"userId":361}).sort({"time":-1}).limit(100);

6.使用$and多条件查询

db.getCollection('CollectionName').find( {$and:[{"userId":37761},{"domain":"time.com"},{"timestamp":{"$gt":1540483200000,"$lte":1540550100000}}]});

mongodb中对应的范围标识符:

"$lt"===================>  "<"
"$lte"==================>  "<="
"$gt"===================>  ">"
"$gte"==================>  ">="
"$ne"===================>  "!="

7.ISOdate时间范围查询

db.getCollection('CollectionName').find({ "timestamp" : { "$gte" : ISODate("2018-04-20T00:00:00Z")
, "$lt" : ISODate("2018-04-21T00:00:00Z") }});

8.插入:

db.CollectionName.insert({"url":"www.baidu.com"});
原文地址:https://www.cnblogs.com/zjdxr-up/p/10698505.html