Tag: mongodb query

Mongo / Mongoose – 按date汇总

我有一个mongo / mongoose模式,当查询重新生成文件如 { "_id" : ObjectId("5907a5850b459d4fdcdf49ac"), "amount" : -33.3, "name" : "RINGGO", "method" : "VIS", "date" : ISODate("2017-04-26T23:00:00Z"), "importDate" : ISODate("2017-05-01T21:15:49.581Z"), "category" : "Not Set", "__v" : 0 } { "_id" : ObjectId("5907a5850b459d4fdcdf49ba"), "amount" : -61.3, "name" : "Amazon", "method" : "VIS", "date" : ISODate("2017-03-23T00:00:00Z"), "importDate" : ISODate("2017-05-01T21:15:49.592Z"), "category" : "Not Set", "__v" : […]

在MongoDB中返回具有不同字段和1select查询的整个文档

我有困难编写一个查询,返回整个文档,同时仍然有1个不同的领域。 我已经试图混乱总结,但无济于事。 googlesearch了一段时间之后我已经find了这个文件: 如何有效地执行多个键“独特”? 它描述了我需要的大部分解决scheme,但是我还需要在另一个领域进行筛选。 这是我现在用来查询TempCollection中文档的sorting列表的function。 function getLatestUserData(UserID, callback) { console.log("in func " + UserID); Temp.find({ UId: UserID }).sort({ "created_at" : -1 }).exec( function (err, data) { console.log(data + " " + err); callback(data); }); }; 然而,我无法弄清楚如何在同一时间过滤,分类和清除所有的东西。 这是使用Aggregates的一个尝试,然而,阅读文章已经有相当长的一段时间了,我只是无法弄清楚我需要的语法: function getLatestUserData(UserID, callback) { Temp.aggregate([ {"$group": { "_id": { value: "$value", SerialNumber: "$SerialNumber" } }}, {$filter : […]

查找arrays中位置元素的最大值

我有一个传感器数据的集合,其中每个文档具有以下结构: [{ _id: 592d9b0e276bd2136caacca0, name: 5 light: [ … ], temperature: [ … ], UV: [ … ], RA: [ … ] }] 每个传感器的每个arrays都有100个元素的长度。 例如,我怎样才能find哪个文件名中包含位置为42(温度arrays)的温度值最高的文件? 第一个元素或最后一个元素有很多解决scheme,但我似乎无法find如何解决它的数组的任意位置。 这是我已经有: db.sensors.aggregate([ { $unwind: "$name" }, { $sort: { "temperature": -1} }, { $limit: 1} ]); 但是,正如你所看到的,“{$ sort:{”temperature“:-1}}”的行不起作用。 我真的不明白如何指定我只想sorting温度数组的位置42元素sorting。

我想从一个MongoDB文档返回一个特定的字段值,但是我不断地将作为返回值

我正在尝试返回一个字段值的文件在mongoose。 我有一个配方模式,在其中,我勾画了一个名为“postedBy”的值,基本上是提交食谱的人的_id。 以下是配方模型的代码: let recipeSchema = new Schema({ // I have other values in the schema, but for clarity sake, // I clearly have the field value defined in the schema model. postedBy: { type: String, required: true, index: true } }); 现在这里是我有问题的代码: /// Here I make my function that returns the postedBy field value […]

如何从MongoDB集合中创build并行和顺序的作业队列(FIFO)?

这是我的collections架构: var mongoose = require('mongoose'); var urlSchema = new mongoose.Schema({ url: String, easeDate: { type: Date, index: 1}, created: { type: Date, default: Date.now } }); 我需要用最旧的文档(通过easeDate)来更新它的easeDatedate到«now + some timeout»并获取受影响的文档。 是否有可能在一个请求? 基本上,我想从集合中创build一个类似于队列的东西(“最老”的是下一个,即先进先出)。 删除url(collections中的文件)是由其他条件完成的。 我不想从队列中删除,但通过更新easeDate 。 最后,我想获取可用的url并设置一个超时值,以便下一次返回下一个url。 如果它是最后一个url,则下次返回null ,因为超时。 如果我可以更新它,那么如何查询数据库(mongoose)来获取下一个urleaseDate <Date.now()? 你能举一个JavaScript例子吗?

使用$或$ elemMatch和条件以外的数组

我的基本结构是我有一个会话对象的用户对象,其中包含一个subjectId和小时价格。 User{ defaultHourly: Number, subjects{ [ id: String, hourly: Number ] } } 我使用elemMatch像这样: query.elemMatch("subjects", { "id": { $in: subjects }, "$or": [ { "hourly": { $eq: null } }, // this is my issue { "$and": [ { "hourly": { $ne: null } }, { "hourly": { $gte: price.low, $lte: price.high } } ] […]

从数组获取最新的子文档

我有一个数组。 我想从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 […]

如何编写查询条件的一些对象不能在对象数组和其他对象之一必须在对象数组?

我有学生收集如下的mongo文件: { name: 'XYZ', age: 26, education: [ { title: 'abc', university: 'pqr', grade: 'A' }, { title: 'def', university: 'uvq', grade: 'B' }, { title: 'ghi', university: 'xyz', grade: 'A' }, { title: 'jkl', university: 'pqr', grade: 'B' }, { title: 'mno', university: 'uvw', grade: 'C' } ] }, { name: 'QQQ', age: 26, […]

只对现有字段sorting

我有这样的collections。 只有less数用户具有sortOrder字段 [{ "_id": "59ccf78bde3d14019169c105", "name": "Best FX broker ", }, { "_id": "59ccf78bde3d14019169c102", "name": "Best automated Performance tool ", "sortOrder": 1, }, { "_id": "59ccf78bde3d14019169c104", "name": "Best Regtech/ reporting solution ", }, { "_id": "59ccf78bde3d14019169c106", "name": "NEW: BEST CRYPTO SERVICE PROVIDER", "sortOrder": 2, }] 当我按sort({"sortOrder": 1})我得到这个 [{ "_id": "59ccf78bde3d14019169c105", "name": "Best FX broker ", […]