These are chat archives for Automattic/mongoose

15th
Aug 2018
Maurizio Bellemo
@mbellemo_twitter
Aug 15 2018 07:05
@lineus
I do not call save(), I update the object (adding an embedded object inside the array)
router.post('/', authenticate(['user', 'admin']), (req, res) => {
    const body = _.pick(req.body, ['name']);

    body.owner_id = req.user._id;

    Company.findOneAndUpdate({
        _id: req.user.company_id
    },
        { $push: { projects: body } },
        { new: true }).then((company) => {
            if (!company) {
                return res.status(404).send();
            }
            res.send({ company });
        }, (e) => {
            res.status(400).send();
        });
});
I use the $push attribute
Kev
@lineus
Aug 15 2018 10:53
@mbellemo_twitter you need to set runValidators: true in your query opts to get the behavior your want. Example here
Maurizio Bellemo
@mbellemo_twitter
Aug 15 2018 12:43
@lineus wow. It looks like you saved my life again. I am gonna try it out tonight and let you know. Thanks again!
Jack
@jackmercy
Aug 15 2018 15:24

Hi everyone
I'm a student and need help about foreign key in mongoose
Right now I have 2 schema

const UserSchema = new mongoose.Schema({

    citizen_id: {
        type: String,
        required: true,
        unique: true
    },
    hashPassword: {
        type: String,
        required: true
    }
});

const CitizenSchema = new mongoose.Schema({
    Id_number: {
        type: String,
        required: true,
        unique: true
    },
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    }
});

I want to make field Id_number in collection Citizen will be primary key and field citizen_id of collection User will be the foreign key which will have the same value from the Id_number in Citizen collection.
How can I link those collection fields ?

Kev
@lineus
Aug 15 2018 15:35
@jackmercy is there a reson you're using Id_number instead of the _id field that gets created automatically?
if not, it's a little cleaner to just use _id here's an example
Jack
@jackmercy
Aug 15 2018 15:38
Yes, I have reason to use Id_number because I want to use this Id_number to login the system.
And I also want to check that 1 citizen only have 1 user account.
And If we have other solution, I open mind to take it.
Many thanks
and that citizen will use their Id_number to login.
Jack
@jackmercy
Aug 15 2018 15:56
Thanks @lineus
It's 11:59 PM in my country so I will try it out in the morning.
Mikołaj Chodorowski
@i-Clyde
Aug 15 2018 21:06
Hi, I'm trying to create something like dynamid object with array of messages in mixed field;
json idea:
image.png
where the 5, 7 is the id of receiver
image.png
Any idea how to make it work? I wish this $push can work
Kev
@lineus
Aug 15 2018 21:11
@i-Clyde do you get an error? what does your schema look like?
Mikołaj Chodorowski
@i-Clyde
Aug 15 2018 21:12
messageHistory: [mongoose.Schema.Types.Mixed],
image.png
Kev
@lineus
Aug 15 2018 21:17
@i-Clyde $push goes on the outside, here's an example
Mikołaj Chodorowski
@i-Clyde
Aug 15 2018 21:18
Yeah, I know but I need to push objects into array into object
image.png
image.png
This is working but after new message the value of msg: just getting update instead of push new object with new message
It's hard to explain but google says It's impossible
Kev
@lineus
Aug 15 2018 21:24
@i-Clyde if the array you're pushing into is nested you can use positional syntax in the update doc
Mikołaj Chodorowski
@i-Clyde
Aug 15 2018 21:29
Right, but in your example the 'f' is static, I need the Key to be dynamic but when I am using variable in query mongoose think it is just a name
I'll show you output using your example
Mikołaj Chodorowski
@i-Clyde
Aug 15 2018 21:35
{$push: {'messageHistory.'+dynamicId: {'messages': [{'...
Mikołaj Chodorowski
@i-Clyde
Aug 15 2018 21:36
Yeah something like this, and how you can push items into "f" for example
Oh wait, no I didn't see that declaration in schema
I need something like this:
const schema = new Schema({
  nested: {
    *: [Schema.Types.Mixed]
  }
});
Kev
@lineus
Aug 15 2018 21:41
look at mongoose maps, not sure if you can use Mixed type for it, you'll have to play around with it. I'll take a look after dinner :smile:
Mikołaj Chodorowski
@i-Clyde
Aug 15 2018 21:41
Bon Appetit :P
Mikołaj Chodorowski
@i-Clyde
Aug 15 2018 22:01
Okay, I think this is what I wanted, I removed the 'messages' Array used your dynamic query idea and used map.
This is how db looks after sending msg to 3 different users 😁
image.png

Schema:

  messageHistory: {
    type: Map,
    of: Array
  },

Query:

let update = { $push: {} };
update.$push['messageHistory.'+ to] = [{'you': true, 'msg': escapeHtml(msg.trim()), 'date': Date.now()}];
Player.findOneAndUpdate({'socketToken': socketid}, update).exec().catch((err) => {error('127691'); console.log('[ERROR] Sending msg: '+err)});