mongodb-query

MongoDb Aggregation: How can I group an array-1 based on another array-2 when given array-1 and array-2?

匆匆过客 提交于 2020-07-28 04:20:52
问题 EDIT: My original question was MongoDb Aggregation: Can you $unwind an input document variable in the pipline of a $lookup stage? Consider the code below: {$lookup: { from:"mydoc", let: {"c":"$myArray"}, pipeline: [ {$unwind: "$$c"}, ] as:"myNewDoc" }} How would I unwind c if I wanted to? /////END OF ORIGINAL QUESTION -----EDIT----- From Tom Slabbaert's comment we now know that it is possible to $unwind an input document variable in the pipline of a $lookup stage. But it is not recommended.

MongoDb Aggregation: How can I group an array-1 based on another array-2 when given array-1 and array-2?

倾然丶 夕夏残阳落幕 提交于 2020-07-28 04:20:33
问题 EDIT: My original question was MongoDb Aggregation: Can you $unwind an input document variable in the pipline of a $lookup stage? Consider the code below: {$lookup: { from:"mydoc", let: {"c":"$myArray"}, pipeline: [ {$unwind: "$$c"}, ] as:"myNewDoc" }} How would I unwind c if I wanted to? /////END OF ORIGINAL QUESTION -----EDIT----- From Tom Slabbaert's comment we now know that it is possible to $unwind an input document variable in the pipline of a $lookup stage. But it is not recommended.

MongoDB: Using match with input document variables

混江龙づ霸主 提交于 2020-07-28 00:28:10
问题 Why do I have to use this code: { $match: { $expr: { <aggregation expression> } } } to match a document using a document input variable as opposed to doing: { $match: { <query> } } ? For example: $lookup: { from: "comments", let: { myvar: '$myInputDocVariable'}, pipeline: [ { $match: { $expr: { $and: [ { $eq: [ "$varFromCommentDocument", "$$myvar" ] }, ] } } }, ], as: "returnedValue" } The query above works fine but the query below does not work as expected. Why is this? Does this mean that

MongoDB: Using match with input document variables

≯℡__Kan透↙ 提交于 2020-07-28 00:27:36
问题 Why do I have to use this code: { $match: { $expr: { <aggregation expression> } } } to match a document using a document input variable as opposed to doing: { $match: { <query> } } ? For example: $lookup: { from: "comments", let: { myvar: '$myInputDocVariable'}, pipeline: [ { $match: { $expr: { $and: [ { $eq: [ "$varFromCommentDocument", "$$myvar" ] }, ] } } }, ], as: "returnedValue" } The query above works fine but the query below does not work as expected. Why is this? Does this mean that

How to insert auto increment number in mongoose

回眸只為那壹抹淺笑 提交于 2020-07-26 19:30:26
问题 Tried to insert auto increment number for serial number in mongodb using mongoose and nodejs but not working.Where i want to update my code to find solution.If anyone knows please help to find solution. subs.model.js: const mongoose = require('mongoose'); var subscriberSchema = new mongoose.Schema({ _id: {type: String, required: true}, email: { type: String } }, { versionKey: false, collection: 'subscribers' }); module.exports = mongoose.model('Subscribers', subscriberSchema); data.controller

How to insert auto increment number in mongoose

帅比萌擦擦* 提交于 2020-07-26 19:26:26
问题 Tried to insert auto increment number for serial number in mongodb using mongoose and nodejs but not working.Where i want to update my code to find solution.If anyone knows please help to find solution. subs.model.js: const mongoose = require('mongoose'); var subscriberSchema = new mongoose.Schema({ _id: {type: String, required: true}, email: { type: String } }, { versionKey: false, collection: 'subscribers' }); module.exports = mongoose.model('Subscribers', subscriberSchema); data.controller

How to insert auto increment number in mongoose

好久不见. 提交于 2020-07-26 19:25:08
问题 Tried to insert auto increment number for serial number in mongodb using mongoose and nodejs but not working.Where i want to update my code to find solution.If anyone knows please help to find solution. subs.model.js: const mongoose = require('mongoose'); var subscriberSchema = new mongoose.Schema({ _id: {type: String, required: true}, email: { type: String } }, { versionKey: false, collection: 'subscribers' }); module.exports = mongoose.model('Subscribers', subscriberSchema); data.controller

$lookup foreignField from nested array

一世执手 提交于 2020-07-22 05:52:26
问题 I have these 2 collections: power_usages_month { "_id" : ObjectId("5f1029d4a25f8180630896ad"), "device_sn" : "ST9L0CY4A2AVY7HFWWUE", "switch_id" : ObjectId("5f0ec823580cf34423ccb13d"), "name" : "Lampu", "current" : 0.6299, "time_minutes" : NumberInt(1040), "created_at" : ISODate("2020-07-16T17:20:04.403+0000"), "updated_at" : ISODate("2020-07-16T17:20:04.403+0000") } { "_id" : ObjectId("5f1029d4a25f8180630896ae"), "device_sn" : "ST9L0CY4A2AVY7HFWWUE", "switch_id" : ObjectId(

$lookup foreignField from nested array

↘锁芯ラ 提交于 2020-07-22 05:52:20
问题 I have these 2 collections: power_usages_month { "_id" : ObjectId("5f1029d4a25f8180630896ad"), "device_sn" : "ST9L0CY4A2AVY7HFWWUE", "switch_id" : ObjectId("5f0ec823580cf34423ccb13d"), "name" : "Lampu", "current" : 0.6299, "time_minutes" : NumberInt(1040), "created_at" : ISODate("2020-07-16T17:20:04.403+0000"), "updated_at" : ISODate("2020-07-16T17:20:04.403+0000") } { "_id" : ObjectId("5f1029d4a25f8180630896ae"), "device_sn" : "ST9L0CY4A2AVY7HFWWUE", "switch_id" : ObjectId(

How do I filter to return a subset of a mongoose schema?

我只是一个虾纸丫 提交于 2020-07-18 21:30:15
问题 My objective is to query mongoDB for a subset of an array within an array. For example, in the 'whole Data Set' (below), I would like to get all of the chorePerson records that have a personID of '5c6e3c74b9f5ed0016b00577' Whole Data Set [ { "_id": "5c7464a26b47a13470411031", "affiliation": "liss_family", "year": 2019, "weekNumber": 9, "chart": [ { "chorePerson": [ { "_id": "5c7464a26b47a13470411054", "person": "emily", "personID": "5c6e3c74b9f5ed0016b00577", "chore": "Catbox", "choreID":