These are chat archives for Automattic/mongoose

9th
Jan 2018
Luciano
@LucianoVandi
Jan 09 2018 18:53
Hi there! I've defined a model with mongoose.model(). This model hold a subdocument as an array of objects...
I don't understand how to update the docs and its subdocs creating an model with new MyModel({...})
I want to update the subdocs with $push
Luciano
@LucianoVandi
Jan 09 2018 18:59
I'm doing something like: MyModel.findByIdAndUpdate(id, new MyModel({field1: 'data', $push: { subdoc: object } }))
This is updating parent document, but set the subdoc as empty...
Caleb B
@zikeji
Jan 09 2018 20:01
@LucianoVandi I haven't done that in particular, but why are you using new MyModel() in your update query?
You need to use the $ identifier - see here: https://docs.mongodb.com/manual/reference/operator/update/positional/
Caleb B
@zikeji
Jan 09 2018 20:08
MyModel.findByIdAndUpdate(id, {
  field1: 'data',
  $push: {
    subdoc: subdocObject
  }
})
and my bad, you don't need $. What you have should work, just without the "new MyModel"
Luciano
@LucianoVandi
Jan 09 2018 20:11
@zikeji thanks, this way works... I'm using new MyModel() to use sync validation...
I mean...
let model = new MyModel({...});
let errors = model.validateSync();
kvidzibo
@kvidzibo
Jan 09 2018 20:44

Hi, I am looking at http://mongoosejs.com/docs/populate.html
example:

Story.
  find({ author: author._id }).
  exec(function (err, stories) {
    if (err) return handleError(err);
    console.log('The stories are an array: ', stories);
  });

my question would be can I found Stories using other authors field not _id?

Kev
@lineus
Jan 09 2018 22:17
@kvidzibo yes you can.
kvidzibo
@kvidzibo
Jan 09 2018 23:10
@lineus how?
Kev
@lineus
Jan 09 2018 23:16
heres a gist of something I was playing with the other day.
@kvidzibo ^
kvidzibo
@kvidzibo
Jan 09 2018 23:19

@lineus https://gist.github.com/lineus/14ae44fce34e64e28eebafa481fad59f#file-populatedb-js-L59 you know that you can do just

addPerson(william).then(function (person) {
  return addPerson(sue)
}).then(function (person) {
  return addPerson(hunter)
}).then(function (person) {
  return addPerson(madison)
}).then(function (person) {
  process.exit(0)
})

right?

Kev
@lineus
Jan 09 2018 23:20
the populate call is in the last file on the page usePopulate.js i think.
it's a super simple example, but I think it answers your question about being able to find by a field other than _id. plus it shows the field selection of the populate call which only returns the names.
kvidzibo
@kvidzibo
Jan 09 2018 23:22
yeah I don't think it's in any way relevant to my question
Kev
@lineus
Jan 09 2018 23:22
Screenshot 2018-01-09 18.22.07.png
that's what gets rendered
I must have misunderstood your question. can you clarify for me?
kvidzibo
@kvidzibo
Jan 09 2018 23:26
yeah as I said I don't think it's related basically I would want to query Child and get all the children where parent is f.e. William
and yes I can do that by getting william and populating children
however if we look at bigger picture what if I will have 1000 williams and I want to all the children maybe only 3 of them have children
so in sql way.. I would query children left join parent and select where parent is william
and I see that in mongoose you can do that only on _id
Kev
@lineus
Jan 09 2018 23:31
ah, that makes sense. I think I remember reading or watching a mongo video a while back that talked about that. thanks for clarifying it for me.
kvidzibo
@kvidzibo
Jan 09 2018 23:32
I think I'll have to use sub documents
Kev
@lineus
Jan 09 2018 23:41
I just saw your code snippet above, gitter's UI is weird. when I scrolled the page, the image above it resized and there it was underneath :)
yeah, I was trying to focus on something else at the time (populate and ejs) and trying to circumvent my tendency to yak shave.