本文為博主原創,轉載請注明出處:
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"});