Tag: aggregation framework

mongooseselectsubdoc领域

我有这样的模式: mongoose.model "Ticket", { title: String body: String likes: Number comments: [{ body: String upvotes: Number downvotes: Number }] } 我的代码来查询 q = @model.findOne {"comments._id": oid}, {"comments.$": 1} q.select "comments.upvotes" q.exec (err, result) => console.log(result.comment[0].downvotes) # 6 正如你所看到的,select不能用于subdocs,它也不会返回选定的字段。 如何解决这个问题?

node.js / mongoDB:如果field为null,则返回自定义值

我想要做的是查询我的数据库文件,如果没有设置字段,返回一个自定义值而不是它。 想象一下,我有一个songs集合,其中一些有字段pathToCover设置和一些不。 对于这些,我想要返回的URL到一个占位符图像,我存储在一个configurationvariables。 我使用express和mongoose运行node.js和mongoDB。 我不确定这是什么最好的方法。 一个明显的解决scheme是查询文档,然后在callback中遍历它们以检查是否设置了字段。 但是这感觉是多余的。 目前,我的代码如下所示: exports.getByAlbum = function listByAlbum(query, callback) { Song.aggregate({ $group: { _id: '$album', artists: { $addToSet: '$artist' }, songs: { $push: '$title' }, covers: { $addToSet: '$pathToCover'}, genres: { $addToSet: '$genre'}, pathToCover: { $first: '$pathToCover'} } }, function (err, result) { if (err) return handleError(err); result.forEach(function(album) { if ( […]

如何在一个MongoDB集合(使用mongoose)中总结一个字段的不同值

想象一下,我有一个名为期刊的集合,其中包含以下文档: { "article": "id1", "d": 2 }, { "article": "id1", "d": 2 }, { "article": "id1", "d": 3 }, { "article": "id2", "d": 2 }, … 其中d是一种开关, 文章是一个参考。 现在我想要得到如下结果: [ { "_id": "id1", "total": 3, "d2": 2, "d3": 1 }, { "_id": "id2", "total": 1, "d2": 1, "d3": 0 } ] 我正在使用mongoose,并有一个名为“ 日记”的模型。 我到目前为止是… Journal.aggregate( […]

计算一个字段在一个集合中出现的次数,同时保留所有没有重复的数据

比方说,我有这样一个集合: { _id : 544e97123c9ef694fc68e21b, title: "First Title", notebook: { title: "Misc", slug: "misc" } } { _id: 54ab035a849788d0921d8eb2, title: "Second Title", notebook: { title: "Personal", slug: "personal" } } { _id: 544e97123c9ef694fc68e21b, title: "Third Title", notebook: { title: "Misc", slug: "misc" } } 在我看来,我希望能够显示一个笔记本标题已经被使用了多less次,以及与特定笔记本的slu link连接,没有特定的顺序。 例如: <a href="/notebooks/misc">Misc(2)</a> <a href="/notebooks/personal">Personal(1)</a> 我通过浏览每个文档来达到这个目的,但问题在于它是因为它正在经历每个文档而重复的。 所以我认为它看起来像这样: <a href="/notebooks/misc">Misc(2)</a> […]

如何在数据库中添加一个字段

我试图用相同的事件名称来分组所有的字段,并且总结这些字段的参与者。 { "_id": { "$oid": "54b6ae8aec17b5e905a5813a" }, "person": { "name": "Something", "phone": "(011) 9468-1319", "email": "myemail@gmail.com", "obs": "" }, "event": { "full": "20/11/01 Classic", "date": "20/11/01", "name": "Special", "participants": 10 }, "__v": 0 } { "_id": { "$oid": "54b6aeaad7e410ec05dd1ca2" }, "person": { "name": "Something", "phone": "(011) 9468-1319", "email": "myemail@gmail.com", "obs": "" }, "event": { "full": […]

一个请求中有多个聚合函数

我有一个数据集是这样的: { item: '123', array: [{ array2:[{ array3: [{ property1: 1234 }] }], anotherArray: [{ property2: 1234 }] }] } 我试图在同一个请求中聚合property2和property之和。 这是我目前的聚合函数: Item.aggregate([ {$match: {itemId: 1234}}, {$unwind: "$array"}, {$unwind: "$array.array2"}, {$unwind: "$array.array2.array3"}, {$unwind: "$array.anotherArray"}, {$group: { _id: 0, property1: {$sum: '$array.array2.array3.property1'}, property2: {$sum: '$array.anotherArray.property2'} }}, {$project: { _id: 0, property1: "$property1", property2: "$property2", }}, ], […]

ID大于10的文档的值域的平均值

我使用express,nodejs和尚。 集合由对象组成: {_id, valueField} 假设_id在插入时被覆盖,并且收集范围从1到5的5个文档的id都是整数。 这就是我现在正在尝试 – 但我相信语法是不正确的。 该函数返回错误500,甚至不会到达console.log(e)。 我想查找_id大于10的集合中的所有对象。 collection.aggregate([ { $match: { "_id":{$gt: 3} }}, { $group: { _id: null, flow: { $avg: "$value" } }} ], function (e, docs) { if (e) { console.log(e); return; } console.log(docs); res.json(docs); }); 我之前写的一个函数来获取id高于10的所有元素都可以正常工作: collection.find({"_id":{$gt: 3}}, {}, function(e,docs){ res.json(docs); }); collections内容: { "_id": 1, "value": 10 […]

如何基于数组元素进行匹配和sorting

var UserSchema = Schema ( { android_id: String, created: {type: Date, default:Date.now}, interests: [{ type: Schema.Types.ObjectId, ref: 'Interests' }], }); Users.aggregate([ { $match: {android_id: {$ne: userID}, interests: {$elemMatch: {$in: ids}} }}, { $group: { _id: { android_id: '$android_id'},count: {$sum: 1}}}, { $sort: {count: -1}}, { $limit: 5 }], 我需要find与我共同最感兴趣的用户(ids数组)的前5个android_ids。 我可以使用来自interest数组的唯一匹配元素的数组。

mongodb / mongoose聚合或返回null

为什么这不起作用? 我无法使用$或条件工作。 但是,当我简单地把两个正则expression式Expessions放在一个联合声明中时,它会返回结果。 Zip.aggregate( { $or: [ {$match: {'ort': regExp}}, {$match: {'ortsteile.ortsteil': regExp}} ] }, function(err, results) { //returns null }); // while this returns results: Zip.aggregate( {$match: {'ort': regExp, 'ortsteile.ortsteil': regExp}}, function(err, results) { //returns a list of results });

mongoose聚合:返回不_id字段

这些是我的文件: {shopId: "aaa", customerId: "bbb", customerName: "xxx", value: 12} {shopId: "aaa", customerId: "ccc", customerName: "yyy", value: 12} {shopId: "aaa", customerId: "bbb", customerName: "xxx", value: 12} {shopId: "ddd", customerId: "bbb", customerName: "xxx", value: 12} 我想知道给定客户在选定的店铺中花了多less钱。 我知道该怎么做: Docs.aggregate( [{ $match: { shopId: selectedShop } }, { $group: { _id: "$customerId", totalVisits: { $sum: 1 }, totalValue: { […]