Tag: aggregation framework

将$ geoNear与另一个集合结合

我有2个collections, resto和meal (每个餐文件有它所属的resto id)。 我想要取得至less有一顿饭的附近的restos 。 现在,我可以去附近的餐馆,但我怎样才能确保他们至less有一顿饭? restoModel.aggregate([{ "$geoNear": { "near": { "type": "Point", "coordinates": coordinates }, "minDistance": 0, "maxDistance": 1000, "distanceField": "distance", "spherical": true, "limit": 10 // fetch 10 restos at a time } }]); 示例resto文档: { _id: "100", location: { coordinates: [ -63, 42 ], type: "Point" }, name: "Burger King" } Sample […]

从数组获取最新的子文档

我有一个数组。 我想从history 数组 (复数)中selectrevision号最高的对象。 我的文档看起来像这样(通常它不仅仅是uploaded_files一个对象): { "_id" : ObjectId("5935a41f12f3fac949a5f925"), "project_id" : 13, "updated_at" : ISODate("2017-07-02T22:11:43.426Z"), "created_at" : ISODate("2017-06-05T18:34:07.150Z"), "owner" : ObjectId("591eea4439e1ce33b47e73c3"), "name" : "Demo project", "uploaded_files" : [ { "history" : [ { "file" : ObjectId("59596f9fb6c89a031019bcae"), "revision" : 0 } ], "_id" : ObjectId("59596f9fb6c89a031019bcaf") "display_name" : "Example filename.txt" } ] } 我select文件的代码: function getProject(req, projectId) […]

根据对分页子项中父项的引用查找文档

有2个集合Movie和Rank Movie 。这2个集合有引用关系 。 Movie model var mongoose = require('mongoose'); var movieSchema = new mongoose.Schema({ m_tmdb_id: { type: Number, unique: true, index: true }, m_adult: { type: Boolean }, m_backdrop_path: { type: String, }, m_title: { type: Number }, m_genres: { type: Array } }); var MovieModel = mongoose.model('Movie', movieSchema); module.exports = { movie: MovieModel […]

在MongoDB中基于时间段获取数据

如何按时间段按date显示计数数据的数量。 我们有插槽 [{'startTime':“2017-04-20T08:30:00.000Z”,“endTime”:“2017-04-20T09:30:00.000Z”}, {'startTime':“2017-04-20T09:30:00.000Z”,“endTime”:“2017-04-20T10:30:00.000Z”}, {'startTime':“2017-04-20T10:30:00.000Z”,“endTime”:“2017-04-20T18:30:00.000Z”}] input [{ "createdAt" : ISODate("2017-04-20T09:20:00.167Z"), }, { "createdAt" : ISODate("2017-04-20T14:20:00.167Z") }, { "createdAt" : ISODate("2017-04-20T11:20:00.167Z") }, { "createdAt" : ISODate("2017-04-20T14:20:00.167Z") }, { "createdAt" : ISODate("2017-04-20T12:20:00.167Z") }] 以这种格式显示数据 产量 [{'startTime':“2017-04-20T08:30:00.000Z”,“endTime”:“2017-04-20T09:30:00.000Z”,“count”:1}, {'startTime':“2017-04-20T09:30:00.000Z”,“endTime”:“2017-04-20T10:30:00.000Z”,“count”:0}, {'startTime':“2017-04-20T10:30:00.000Z”,“endTime”:“2017-04-20T18:30:00.000Z”},'count':4]

与子文档项目的计算和相加

我有发票模型如下 { … "itemDetails": [ { "item": "593a1a01bbb00000043d9a4c", "purchasingPrice": 100, "sellingPrice": 150, "qty": 200, "_id": "59c39c2a5149560004173a05", "discount": 0 } ], "payments": [], … } 我需要计算项目总价或销售价格 – 折扣。 我看到了$sum操作符,但找不到与item._id

如何使用$ filter(聚合)来select一些数组的字段只有条件为真?

在这里,我会告诉你我到底想要什么。 假设我有以下两个文件为XYZ模型。 [ { "_id" : ObjectId("59ef8786e8c7d60552139ba9"), "name" : "s1", "email" : "one@one.com", "mobileNumber" : "910123456989", "verificationStatus" : true, "activities" : [ { "name" : "a1", "_id" : ObjectId("59ef8786e8c7d60552139bae"), "type" : 0, "level" : null, "verificationStatus" : true }, { "name" : "a2", "_id" : ObjectId("59ef8786e8c7d60552139bad"), "type" : 0, "level" : null, "verificationStatus" : false […]

mongoose聚集date范围

我正在尝试mongoose聚集。 我想获得date范围内的字段的总和。 http://mongoosejs.com/docs/api.html#model_Model.aggregate mymodel.aggregate({$match:{date:{$gte: fromDate, $lt: toDate}}}, {$project:{_id:0,date:1, count:1}}, {$group:{'_id':0,count:"$count"}}).exec(function(err,data){ if(err){ console.log('Error Fetching model'); console.log(err); } callback(false, data); }); 这不起作用。 { [MongoError: exception: the group aggregate field 'count' must be defined as an expression inside an object] name: 'MongoError', errmsg: 'exception: the group aggregate field \'count\' must be defined as an expression inside an object', […]

如何在Mongoose / NodeJS的时间戳字段中计数不同的date项目?

我在我的NodeJS中使用Mongoose,并且有一个文档看起来像这样的集合 – var SomeSchema = new Schema({ date: { type: Date, default: new Date() }, some_item: { type: String } }); date字段包含时间部分的date(例如, 2014-05-09 09:43:02.478Z )。 2014-05-08如何获得给定date的不同项目数? 编辑:理想情况下,我想获得每个不同date的logging计数。

Mongoose / MongoDB聚合 – $匹配$ unwind $匹配不工作?

我将如何改变这个档案视图聚合也聚合from.view.archive:真? ..同时防止重复消息的聚合..如果从用户(sessionUser)存在于数组中,则只能得到这个消息之一.. if (archive === true) { console.log('archive === ' + true); Models.Message.aggregate([ // Match documents { "$match": { "to": { "$elemMatch": { "username": req.session.username, "view.archive": true, "view.bin": false } }, "$or": messagingquery }}, // Unwind to de-normalize { "$unwind": "$to" }, // Match the array elements { "$match": { "to.username": req.session.username, "to.view.archive": true, "to.view.bin": […]

Node.js – 如何计算在MongoDB中的子文件的总和(总和)?

我的collections结构是, [{ "_id": "….", "name": "aaaa", "level_max_leaves": [{ level: "ObjectIdString 1", max_leaves: 4, }] }, { "_id": "….", "name": "bbbb", "level_max_leaves": [{ level: "ObjectIdString 1", max_leaves: 4, }] }, { "_id": "….", "name": "cccc", "level_max_leaves": [{ level: "ObjectIdString 1", max_leaves: 7, }] }, { "_id": "….", "name": "dddd", "level_max_leaves": [{ level: "ObjectIdString 2", max_leaves: 3, […]