Tag: aggregation framework

如何通过条件匹配子文档中的数组元素

我有以下Mongoose模式: EmployeeSchema: var EmployeeSchema = new Schema({ name : String, employeeDetailsId: { type: Schema.Types.ObjectId, ref: 'employeedetails' } }); EmployeeDetailSchema: var EmployeeDetailSchema = new Schema({ employeeId: { type: Schema.Types.ObjectId, ref: 'employee' }, primarySkills: [ { type: Schema.Types.ObjectId, ref: 'skills' } ], }); SkillsSchema: var SkillsSchema = new Schema({ name: { type: String, required: true } }); […]

在嵌套数组中search多个值

我有以下集合: { _id: 1, docs: [{ name: file1, labels: [label1, label2, label3, label4] },{ name: file2, labels: [label3, label4] },{ name: file3, labels: [label1, label3, label4] }] } 当用户search标签时,我需要获取所有包含这些标签的文档。 所以如果用户search“label1”和“label3”,我需要得到“file1”和“file3”。 目前我有以下代码: var collection = db.collection(req.user.username); collection.aggregate([ // Unwind each array { "$unwind": "$docs" }, { "$unwind": "$docs.labels" }, // Filter just the matching elements { […]

想在mongobd node.JS中通过_id得到参考集合的对象

我想通过引用id集合来获取特定logging的所有细节。 { "_id" : ObjectId("586c8bf63ef8480af89e94ca"), "createdDate" : ISODate("2017-01-04T05:45:26.945Z"), "branch" : { "$ref" : "branchmst", "$id" : "5864ac80fa769f09a4881791", "$db" : "eviral" }, "__v" : 0 } 这是我的collectionslogging。 我需要所有来自“branchmst”集合的详细信息,其中“_id”是“5864ac80fa769f09a4881791”。

可能? $添加数组的值与对象总值进行比较

mongodb native for node.js(驱动程序版本是2.2.4和MongoDB shell版本:3.2.9) 我的collections有这样的对象: {x:[{v:0.002},{v:0.00002}],t:0.00202} //<this one has the full total in its values {x:[{v:0.002},{v:0.002}],t:0.00202} {x:[{v:0.002},{v:0.002}],t:0.3} (这里显示没有他们的对象id) 我不确定如何将所有xv加起来,只返回xv总和大于或等于对象t的对象 aggregate({"t":{"$gte":{"$add":["xv"]}}}) 返回每一个对象,我没有任何其他的想法阅读文档的语法的顺序。 mongodb甚至可以在查询中做到这一点?

如何从mapReduce mongoose中的关键字获得近似值?

我在MongoDB中有一些文件,例如: { "_id" : ObjectId("587f41ffb5ffa43ec25aa075"), "IP" : "113.183.149.127", "Product_Version" : "CMC_2.0", "Product_ID" : "HHFFHJRLAUHOZDBM1YWY291NM0DD7J40", "OS" : "Microsoft_Window_7", "virus" : "Adware:W32/Superfish", "Time" : ISODate("2017-01-18T10:22:55.356Z"), "city_name" : "", "location" : { "latitude" : 0.0, "longitude" : 0.0 } } { "_id" : ObjectId("587f41ffb5ffa43ec25aa078"), "IP" : "27.76.109.254", "Product_Version" : "CMC_2.0", "Product_ID" : "UCOV8JY5YHKQ2B0HPCC0IM1S9T0ICWL7", "OS" : "Microsoft_Window_7", "virus" : […]

使用switch case更新集合中的多个文档

我在我的NodeJS应用程序中使用MongoDB本地驱动程序。 我在我的数据库中有一个需要更新的shifts集合。 在我的class次集合中的示例文档 { "_id" : ObjectId("588425105560bd2ba0065fa4"), "from" : ISODate("2017-01-23T03:20:00.000Z"), "to" : ISODate("2017-01-23T06:20:00.000Z"), "jobId" : ObjectId("586efda790541421b0432897"), "hourlyRate" : 15 } { "_id" : ObjectId("588425105560bd2ba0065fa5"), "from" : ISODate("2017-01-25T03:20:00.000Z"), "to" : ISODate("2017-01-25T06:20:00.000Z"), "jobId" : ObjectId("586efda790541421b0432897"), "hourlyRate" : 15 } 我需要做的是以下 – 更新符合条件的所有文档的hourlyRate : 匹配jobId(很简单) 设置hourlyRate = 20如果from是Weekday 设置hourlyRate = 25如果from星期六开始 设置hourlyRate = 30如果from星期天开始 我想尽可能在​​单个查询中完成。 我的解决scheme迄今为止 使用开关大小写,并使用date聚合函数中的$dayOfWeek确定datetypes。 但是,我无法将交换机与updateMany结合使用。 […]

计算Mongo中引用的对象的数量

给定两个集合,如: users: name posts: title user_id 如何有效地查询超过5个post的用户? 我可以使用$lookup进行聚合,但是对于大型数据集来说需要很长时间。 另一个select是维护users的“postCount”,但我想避免必须保持这一点。 我在节点上使用Mongoose。

是否有可能更新查询中的集合中的值?

我不确定标题是否解释了我想说的。 但是这里假设集合的结构是这样的; [ {email: "alex@sda.com"}, {email: "elizabeth@sds.com"}, {email: "hannah@xx.com"}, ] 我想收到电子邮件,但在@字符之前。 我做了什么来做到这一点? db.collection.find({}).toArray(function(err, result){ var emails = []; for(var i =0; i< result.length; i++){ emails.push(result[i].split("@")[0]); } }) 但是,我不觉得这是有效的,因为在查询之后,我遍历结果并将它们存储在新数组中。 有没有更好的办法,只有alex , elizabeth , hannah查询? 这只是一个例子。 我想问你的build议,因为我有很多这样的情况。 (我正在寻找一种最有效的修剪方式,更新集合中的一些值)

在数组元素中添加一个子文档

下面的函数返回“结果”参数中的一个数组: module.exports.get = function (req, res) { objModel.find(function (err, results) { res.json({ record: results }) }) }; 我想将其引用集合logging列表添加到每个元素的一个新对象中。 就像下面这样: for (var i = 0; i < results.length; i++) { module.exports.get = function (req, res) { objModel.find({ _id: results[i]._id }, function (err, record) { results[i]["objNew"] = record }) }; } 我的完整代码如下所示: module.exports.get = function (req, res) […]

mongoose无效的运营商$匹配

我将根据email_address,firstname,lastname,type和phone_number来处理用户search。 电话号码search将精确search与国家代码,而其他人将包含&区分大小写的search。 所以,我写了下面的代码。 User.aggregate( [ { "$redact": { "$cond": [ { "$and": [ { "$match": { type: req.body.type, email_address: new RegExp((req.body.email_address || req.body.any || '').toLowerCase(), "i"), "firstname": new RegExp((req.body.firstname || req.body.any || '').toLowerCase(), "i") , "lastname": new RegExp((req.body.lastname || req.body.any || '').toLowerCase(), "i") } }, { "$or": [ { "$setIsSubset": [ [ { "$substr": [ […]