js对mongoDB进行查询操作

查询操作

查询一个集合下的所有记录

db.message.find({})

根据字符串条件查询指定记录

db.message.find({"content":"今天天气真的很好很好"})

查询包含某个字符串的

db.message.find({"content":/真的/})

查询以某个字符串开头的

db.message.find({"content":/^今天/})

根据正则表达式查询

db.message.find({"content":{$regex:/真的/}})

查询时呼略大小写

db.message.find({"_class":/^COM.dgg/i})

查询某一天的数据

db.message.find({"createTime":{"$gte":ISODate("2018-05-23T00:00:00Z"),"$lt":ISODate("2018-05-24T00:00:00Z")}})

查询某元素非空的数据

db.message.find({userName:{"$ne":null}})

in关键字查询

db.message.find({userName:{$in:["小无","caiufwen"]}})

根据数组长度查询

db.message.find({praises:{"$size":2}})

根据数组元素中的元素进行查询

db.message.find({"praises":{$elemMatch:{"user":"caifuwen"} }})

根据自身属性关系查询

db.message.find({$where:"this.replies.length == this.praises.length"})

多个条件查询

db.message.find({"createTime":{"$gte":ISODate("2018-05-23T00:00:00Z"),"$lt":ISODate("2018-05-24T00:00:00Z")},"praises":{$elemMatch:{"user":"caifuwen"}}})

结果中只包含指定的列

db.message.find( {}, { _id: 1, content: 1 } )

结果中不包含指定的列

db.message.find( {}, { _id: 0, content: 0 } )

正序排序

db.message.find().sort({createTime:1})

倒序排序

db.message.find().sort({createTime:-1})

skip和limit

db.message.find().sort({createTime:1}).skip(1).limit(2)

查询praises数组中包含user属性为caifuwen的元素的消息

db.message.find( { "praises": { $elemMatch: { user: "caifuwen" } } } )

聚合查询

db.cloud_message_comment.aggregate(
    [
        {   $group:{
                _id:{messageId:"$messageId",commentType:"$commentType"},
                commentId:{$push:"$_id"},
                lastCommentTime:{$max:"$createDate"}
            }
        }
    ]
)

聚合查询可以参考一下两个页面的内容

http://www.runoob.com/mongodb/mongodb-aggregate.html

https://blog.csdn.net/wuya814070935/article/details/48049447


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mingo 是 MongoDB 查询语言的 JavaScript 实现。Mingo 利用 MongoDB 风格查询,在客户端或者服务器端环境下,允许直接查询内存的 JavaScript 对象。特性:Comparisons Operators ($gt, $gte, $lt, $lte, $ne, $nin, $in)Logical Operators ($and, $or, $nor, $not)Evaluation Operators ($regex, $mod, $where)Array Operators ($all, $elemMatch, $size)Element Operators ($exists, $type)Aggregation Pipeline Operators ($group, $match, $project, $sort, $limit, $unwind, $skip)Conditional Operators ($cond, $ifNull)Group Operators ($addToSet, $sum, $max, $min, $avg, $push, $first, $last)Arithmetic Operators ($add, $divide, $mod, $multiply, $subtract)String Operators ($cmp, $strcasecmp, $concat, $substr, $toLower, $toUpper)Set Operators ($setEquals, $setIntersection, $setDifference, $setUnion, $setIsSubset, $anyElementTrue, $allElementsTrue)Projection Operators ($elemMatch, $slice)JSON stream filtering and projection. NodeJS only使用var Mingo = require('mingo'); // or just access *Mingo* global in browser // setup the key field for your collection Mingo.setup({     key: '_id' // default }); // create a query with criteria // find all grades for homework with score >= 50 var query = new Mingo.Query({     type: "homework",     score: { $gte: 50 } });搜索和过滤// filter collection with find() var cursor = query.find(collection); // shorthand with query criteria // cursor = Mingo.find(collection, criteria); // sort, skip and limit by chaining cursor.sort({student_id: 1, score: -1})     .skip(100)     .limit(100); // count matches cursor.count(); // iterate cursor // iteration is forward only while (cursor.hasNext()) {     console.log(cursor.next()); } // use first(), last() and all() to retrieve matched objects cursor.first(); cursor.last(); cursor.all(); // Filter non-matched objects ( var result = query.remove(collection); 标签:Mingo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值