These are chat archives for Automattic/mongoose

6th
Aug 2018
Sergey Reus
@rocknrolla777
Aug 06 2018 11:16
Hi! Does new version of mongoose support transactions?
Kev
@lineus
Aug 06 2018 11:18
@rocknrolla777 yes. this blog post has a good example about half way down the page.
Ambika Kumar
@ambikarajbadal
Aug 06 2018 11:35
How can I use express validator with request body?
Note : - I have many variables which values comes through the various logic.
Charlie
@charliecode
Aug 06 2018 14:41
Any experts on .set() in here. I’m having problems saving new things to DB after using .set() to create new keys in the model. Obviously the keys are not in the schema which is why I’m using .set() to create them dynamically.
Kev
@lineus
Aug 06 2018 14:43
@charliecode once you compile the schema into a model with mongoose.model() you can't add new paths to it.
Charlie
@charliecode
Aug 06 2018 14:44
@kev not sure I’m following 100%. I am succuessfully creating new keys with .set()
the problem I’m having is using .save() to save the values after I’m successfully creating the new keys, but the keys are def successfully being added
Kev
@lineus
Aug 06 2018 14:46
when you call .set() on the Model, it's not adding it to the schema, which means it won't get saved to the db.
you have to add all paths to the schema before compiling it.
Charlie
@charliecode
Aug 06 2018 14:48
@lineus True, but after using .set() I then use .save() and it does successfully add the new keys to the DB, this is verified 100%. But the problem I’m having is after I successfully create the new keys in the DB I then try to add new values to those new keys. This is where it is not saving.
here is schema

import mongoose from "mongoose";
const Schema = mongoose.Schema;

const shipmentEvents = new Schema({
userId: String
}, { strict: false });

export const ShipmentEventList = mongoose.model('Shipment_event_list', shipmentEvents);

here is where I save the new keys
// new schema model
let newShipmentEventListCollection = new ShipmentEventList();
                // set and create yearly data model in the DB
                newShipmentEventListCollection.set('shipmentEventList', data);
                newShipmentEventListCollection.userId = user.authentication.userId;

                // save yearly data model in DB
                newShipmentEventListCollection.save((err) => {

                    // if err
                    if (err) { return reject('DB error.'); }

                    // success
                    resolve(data);
                });
Kev
@lineus
Aug 06 2018 15:01
oh, I see, you're using strict: false
Charlie
@charliecode
Aug 06 2018 15:01
yes exactly
Kev
@lineus
Aug 06 2018 15:05
@charliecode how are you trying to update the existing docs in the db? can you share that bit too? I'll see if I can reproduce it.
Charlie
@charliecode
Aug 06 2018 15:09
@lineus That’s exactly what I’m having problems doing. Actually getting it to save to the existing doc I keys I created
Tried tons of stuff, how do you suggest updating?
It always shows it’s in the doc but when I try ti .save() the new stuff never goes to DB
Charlie
@charliecode
Aug 06 2018 15:11
awesome let me check it out
@lineus What is the dropDatabase() func for? Is it necessary for this to work?
Kev
@lineus
Aug 06 2018 15:15
no, it's just my way of making sure that the db is empty when I run the test. I use /test a lot with different indexes etc.
Charlie
@charliecode
Aug 06 2018 15:15
I see, ok
Charlie
@charliecode
Aug 06 2018 15:42

@lineus Your example is great but not exactly what I’m trying to accomplish. On your first .set() can you set it like this

let data = {
shipmentEvents: {

                    ["2018"]: {
                        Jan: {
                            1: []
                        }
                    }
                }
            };

            test.set('shipmentEventLists', data);
and then what I’m doing is trying to add things to the array afterwards
Sorry never used gitter before, not sure why the markup is screwing up
Kev
@lineus
Aug 06 2018 15:46
try using three backticks on separate lines and using shift-enter to create new lines.
```
code
```
Charlie
@charliecode
Aug 06 2018 15:47
nice thank you
Charlie
@charliecode
Aug 06 2018 15:54
let data = {
                                shipmentEvents: {
                                    ["2018"]: {
                                        Jan: {
                                            1: []
                                        }
                                    }
                                }
                            };

                            test.set(shipmentEventsLists, data);
getting the hang of it lol
Kev
@lineus
Aug 06 2018 15:54
this syntax ["2018"]: { seems wierd to me. What are the brackets for?
Charlie
@charliecode
Aug 06 2018 15:55
@lineus In order to create a new key
with numbers
which I guess is not so important since I out a string there
problem is when you try to use a number it won’t work
number as a key that is
It’s not a big deal
Kev
@lineus
Aug 06 2018 16:15
@charliecode maybe this will help
Charlie
@charliecode
Aug 06 2018 16:17
@lineus Awesome I’ll check it out, thanks so much for your help btw
Kev
@lineus
Aug 06 2018 16:26
when you add fields to documents with strict: false no getters or setters are created for them on the model instances returned from queries as demonstrated here
anytime :) happy to help!
Charlie
@charliecode
Aug 06 2018 16:28
@lineus Yes exactly, I believe the issue came with my not using the toObject() function. I was instead trying to access it by using the ._doc as nothing else was availble since I’m creating on the fly
D3ftp
@Lukasz199312
Aug 06 2018 19:45

Hey this code throw

doc.update({ userOwner: userID, 'series.seriesID': seriesID }, {"$set": {"series.$.currentSeasonID": seasonID,  "series.$.currentEpisodeID": episodeID, "series.$.seriesID":seriesID}});

MongoError: cannot use the part (series of series.seriesID) to traverse the element ({series: [ { currentEpisodeID: 8, currentSeasonID: 4, seriesID: 11 } ]}).

Error do not appear during manually Query (in mongodb)

MongoDB: 3.6
Mongoose: 4.7.3