mongodb-query

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

时光总嘲笑我的痴心妄想 提交于 2020-07-18 21:29:04
问题 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":

Push element into nested array mongoose nodejs

青春壹個敷衍的年華 提交于 2020-07-18 07:26:04
问题 I am trying to push a new element into an array, I use mongoose on my express/nodejs based api. Here is the code for mongoose: Serie.updateOne({'seasons.episodes.videos._id': data._id}, {$push: {'seasons.episodes.videos.$.reports': data.details}}, function(err) { if (err) { res.status(500).send() console.log(err) } else res.status(200).send() }) as for my series models, it looks like this: const serieSchema = new mongoose.Schema({ name: {type: String, unique:true, index: true, text: true},

How yo use arrayFilters with mongoose 5.x.x? [duplicate]

若如初见. 提交于 2020-07-18 02:10:03
问题 This question already has answers here : How to Update Multiple Array Elements in mongodb (15 answers) Closed 2 years ago . I've asked a question couple of days ago about updating arrays in nested arrays of objects. Right now MongoDB 3.6 officially supports it via arrayFilters feature. Is it implemented in Mongoose 5.x.x? What's the syntax? Which method should I use? 回答1: Actually here is an example of findOneAndUpdate command: Company.findOneAndUpdate( {'companyId': parseInt(req.params

mongodb impossible (?) E11000 duplicate key error dup key when upserting

拥有回忆 提交于 2020-07-16 16:55:13
问题 My understanding is that update with upsert:true on a single document is an atomic operation so this should never result in a duplicate key error, especially not on the primary _id key, when the collection has no unique-ly indexed fields: Order.update({ _id: order._id }, query, { upsert: true }, cb) // with mongoose But this appears in the mongod.log: 2015-03-27T09:39:10.349-0400 I WRITE [conn258236] update xyz.orders query: { _id: "6353f880-c6a7-4260-809f-98e0af27b9a2" } update: { $set: { ..

$unset on multiple fields in mongodb

心已入冬 提交于 2020-07-03 00:45:10
问题 Suppose I have a collection in mongoDB like given below - { name : "Abhishek", Roll_no : null, hobby : stackoverflow }, { name : null, Roll_no : 1, hobby : null } Now I want to delete the fields in my Documents where the field values are null. I know that I can do it using the $unset in following way - db.collection.updateMany({name: null}, { $unset : { name : 1 }}); And we could do it in the same way for hobby and name field. But I was wondering if I can do the same deletion operation using

How to get data between the two dates from mongodb using mongoose

烂漫一生 提交于 2020-06-29 05:09:33
问题 My question is simple.Tried to get record from mongodb between the two dates like[ 01-06-2020 to 07-06-2020].But not working.How to do it? Date format is dd-mm-yyyy mongodb records: [ { _id:ObjectId("5edd1df67b272e2d4cf36f70"), date:"01-06-2020", pid:1, pname:"Micheck" }, { _id:ObjectId("5edd1dk67b272e2d4cf31f72"), date:"03-06-2020", pid:2, pname:"Zohn" }, { _id:ObjectId("5edd1rf67b272e2d4cf16f73"), date:"07-06-2020", pid:3, pname:"Robert" }, { _id:ObjectId("5edd1dw67b272e2d4cf76f76"), date:

How to get data between the two dates from mongodb using mongoose

ε祈祈猫儿з 提交于 2020-06-29 05:08:13
问题 My question is simple.Tried to get record from mongodb between the two dates like[ 01-06-2020 to 07-06-2020].But not working.How to do it? Date format is dd-mm-yyyy mongodb records: [ { _id:ObjectId("5edd1df67b272e2d4cf36f70"), date:"01-06-2020", pid:1, pname:"Micheck" }, { _id:ObjectId("5edd1dk67b272e2d4cf31f72"), date:"03-06-2020", pid:2, pname:"Zohn" }, { _id:ObjectId("5edd1rf67b272e2d4cf16f73"), date:"07-06-2020", pid:3, pname:"Robert" }, { _id:ObjectId("5edd1dw67b272e2d4cf76f76"), date:

Mongodb using $text in aggregate query

大城市里の小女人 提交于 2020-06-29 03:38:13
问题 I'm trying to search in text with aggregate and I have the following code.Here is my aggregate query, but doesn't work. I expect to search first for the text and if find posts with that words in it to find user and then to query by user.name and the topic. const posts = await Post.aggregate([ { $match: { $text: { $search: postWords ? `${postWords}` : /.*/, $caseSensitive: false, }, }, }, { $lookup: { from: "users", localField: "user", foreignField: "_id", as: "user", }, }, { $unwind: "$user",

How do I solve: 'MongoError: $where is not allowed in this atlas tier'?

你离开我真会死。 提交于 2020-06-28 07:46:45
问题 How do I solve: 'MongoError: $where is not allowed in this atlas tier' when using MongoDB Atlas? Here is my code: async function getEventsTakingPlace(){ const getEventsTakingPlace = await event.find({$where: function() { return ((this.eventTime < new Date()) && (new Date(this.eventTime).getTime()+ 100*60000 > new Date().getTime() ) ) }}, function(err, events){ if(err){ return err; }else { return events; } }); return getEventsTakingPlace }; when I use my local mongoDB instance it works but

MongoDB lookup array of objects by field (join conditions and uncorrelated sub-queries)

岁酱吖の 提交于 2020-06-28 03:56:41
问题 I don't understand what I'm missing with the aggregation lookup using join conditions and uncorrelated sub-queries. processes collection: { _id: 'p1', steps: [ { _id: 'ps1', step: 's1', time: 10 }, { _id: 'ps2', step: 's2', time: 15 } ] } steps collection (for the document with _id: s1 ): { _id: 's1', name: 'step 1' } Working aggregation (standard one, without join conditions and uncorrelated sub-queries): processes.aggregate([ { // match stage or whatever prior stage }, { $lookup: { from: