These are chat archives for Automattic/mongoose

1st
Mar 2016
Andrew Newdigate
@suprememoocow
Mar 01 2016 09:50
Hi. We're in the process of upgrading to mongo 3.2. I'm very interested in enabling { readConcern: "majority" } on some queries, which is a new feature in mongo 3.2. Does anyone know if this feature is on the mongoose road-map?
J
@jhyland87
Mar 01 2016 16:25
Hey guys, im having an issue trying to ignore documents that are in an array of ObjectIds, heres the query thus far:
this.find({ _id: { '$ne': { '$in': [ '56d5c1e1b77f18d26c6d7cb3', '56d0808066b93cad49dbbbe7' ] } } } })
The error im getting is:
{ [CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"]
  message: 'Cast to ObjectId failed for value "[object Object]" at path "_id"',
  name: 'CastError',
  kind: 'ObjectId',
  value: { '$in': [ '56d5c1e1b77f18d26c6d7cb3', '56d0808066b93cad49dbbbe7' ] },
  path: '_id',
  reason: undefined }
Is it possible to use the $ne and $in together?
J
@jhyland87
Mar 01 2016 16:40

Im basically trying to execute this same query:

db.assets.find({ _id: { $ne: { $in: [ ObjectId("56d0819f655baf4a4a7f9cc9"), ObjectId("56d0819e655baf4a4a7f9cc2") ] } } })

Which works via the cli

J
@jhyland87
Mar 01 2016 16:45
Nevermind! Found the Query#nin operator..
dennisver
@dennisver
Mar 01 2016 17:41
Hi folks, I'm just getting started with Mongoose. One thing I really struggle with is keeping references in sync. Lets say I have 2 models (Blog and Blogpost). Blog has a reference [{blogpots}]. Blogposts has a reference to {blog}. So when I create a Blogpost I reference to {blog}. This all works fine. Getting blogpost.populate('blog') also works fine, but the other way around blog.populate('blogposts') doesn't work because the reference to blogposts isn't saved/updated. Is there any way to sync these references? So that, whenever I update the 'blog' reference on the blogpost model, the post model's blogposts also updates
Daniel Acuña
@moustacheful
Mar 01 2016 18:27
Hello!
I have an app with mongoose and for some reason whenever I try any kind of database interaction I get nothing. No callbacks execute, no errors, no logs, no actual data changed, nothing.
any idea of what could be causing this? I enabled debug mode with mongoose.set('debug',true) but I still get nothing
Daniel Acuña
@moustacheful
Mar 01 2016 18:35
@dennisver how did you set up the references? there shouldn't be any need for syncing since they're just that, references. (unless you for some reason changed the $id of a document)
dennisver
@dennisver
Mar 01 2016 18:36
@moustacheful Thanks for the answer
I just defined them in my schema
Daniel Acuña
@moustacheful
Mar 01 2016 18:37
could you paste that part of the code here?
dennisver
@dennisver
Mar 01 2016 18:37
Okay, just a second
const Blog = mongoose.model('Blog', new Schema({
  title: String,
  blogposts: [{
    type: Schema.Types.ObjectId, ref: 'Blogpost'
  }]
}))

const Blogpost = mongoose.model('Blogpost', new Schema({
  body: String,
  blog: {
    type: Schema.Types.ObjectId, ref: 'Blog'
  }
}))
Daniel Acuña
@moustacheful
Mar 01 2016 18:41
seems about right, and you should be getting the documents associated.
are you sure when updating you're really updating and not saving a new one?
dennisver
@dennisver
Mar 01 2016 18:42
It actually already goes wrong when creating one for the first time
Daniel Acuña
@moustacheful
Mar 01 2016 18:42
try using a tool to check your db (like robomongo: https://robomongo.org/)
oh? what do you mean?
dennisver
@dennisver
Mar 01 2016 18:43
So let’s say I first create a Blog (id: abc123), then I create a Blogpost({body: ‘Hello world’, blog: ‘abc123’})
That all works fine. When Blogpost.find().populate(‘blog’) I get the associated Blog
But Blog.find().populate(‘blogposts’) doesn’t work (because the blogposts field in the database is empty
Daniel Acuña
@moustacheful
Mar 01 2016 18:45
aah, I see what you mean now.
dennisver
@dennisver
Mar 01 2016 18:45
Since I only added the blog to the blogpost, and not the blogpost to the blog
I’d like to know if it’s possible to keep those references in sync
Daniel Acuña
@moustacheful
Mar 01 2016 18:45
yes, I get it. If that were your schema you'd need to update it every time
you could setup hooks on your model to auto update the Blog
dennisver
@dennisver
Mar 01 2016 18:46
Hmm that really sucks haha
Daniel Acuña
@moustacheful
Mar 01 2016 18:47
haha it depends!, I'm not sure your schema makes much sense
dennisver
@dennisver
Mar 01 2016 18:47
Can you explain?
Daniel Acuña
@moustacheful
Mar 01 2016 18:47
ideally you would just have the Post schema, wouldn't you?
why does the Blog model exist in the first place?
dennisver
@dennisver
Mar 01 2016 18:47
Blog and blogposts are just examples
The real code is a bit more complex
However, it’s my first time using a NoSQL database
Daniel Acuña
@moustacheful
Mar 01 2016 18:48
ah okay, just making sure. because just judging by the name of your models, it would mean you add multiple blogs and add posts to them
dennisver
@dennisver
Mar 01 2016 18:48
So I might get it all wrong
Daniel Acuña
@moustacheful
Mar 01 2016 18:48
if you want to create a blog, you would just have a single Post collection
no need for a container or something like that
dennisver
@dennisver
Mar 01 2016 18:49
Well actually it’s more like Enterprise > Managers > Stores
In the real code
Daniel Acuña
@moustacheful
Mar 01 2016 18:50
ah okay, that makes more sense :P
well, yeah, in that case you'll have to stick with stuff like hooks
dennisver
@dennisver
Mar 01 2016 18:50
Hmm that really sucks haha
I tried Sails before, there it works fine
Daniel Acuña
@moustacheful
Mar 01 2016 18:51
it's not as bad as it sounds, try looking into it
dennisver
@dennisver
Mar 01 2016 18:51
But I’m not really satisfied with Sails, so I gave Koa + Mongoose a try
Daniel Acuña
@moustacheful
Mar 01 2016 18:51
but yeah, you need to do the code on your own, no auto sync stuff
dennisver
@dennisver
Mar 01 2016 18:51
Do you know any other ODM/ORM that does sync references?
Daniel Acuña
@moustacheful
Mar 01 2016 18:55
well if you're used with sails and its odm/odm did what you wanted, you could use that
it's called waterline
dennisver
@dennisver
Mar 01 2016 18:55
Haha yea but the amount of time ot takes before they implement new features is ridiculous
That’s why I don’t want to use Sails / Waterline anymore
Daniel Acuña
@moustacheful
Mar 01 2016 18:56
I guess you're out of luck, then :)
dennisver
@dennisver
Mar 01 2016 18:56
Haha ok :D
Thanks anyway for your help
Daniel Acuña
@moustacheful
Mar 01 2016 18:56
still, as far as I know, there's no such thing as auto adding references like you said
actually hmm
why would you need to save the parent reference in post?
dennisver
@dennisver
Mar 01 2016 18:57
I don’t want to use hooks as basically the whole app is just CRUD, so I’d like to have DRY code and having specific hooks for each model just .. argh :D
Since my back-end server only serves json as an API, and having the possibility to /GET blogpost/id?with=blog does make sense or am I wrong?
Daniel Acuña
@moustacheful
Mar 01 2016 19:00
it really depends on what you need :P
you explicitly need the parent on that endpoint?
dennisver
@dennisver
Mar 01 2016 19:01
I don’t know yet
:D
There are use cases that I might need it
Daniel Acuña
@moustacheful
Mar 01 2016 19:02
haha, well, in that case you could still have a route like GET /:blog/:post
and then fetch both the parent and children manually
dennisver
@dennisver
Mar 01 2016 19:02
Haha yea, then I have 2 http requests
Daniel Acuña
@moustacheful
Mar 01 2016 19:03
why 2?
dennisver
@dennisver
Mar 01 2016 19:03
one for getting the blog and another one for getting the post
Since I don’t want my post to automatically include the blog
Daniel Acuña
@moustacheful
Mar 01 2016 19:06
haha, oh well then, I'm not sure how to help
maybe get your requirements first to be sure of what you really need
dennisver
@dennisver
Mar 01 2016 19:07
Haha that’s ok :D thanks for your help anyway
Daniel Acuña
@moustacheful
Mar 01 2016 19:07
no problem
dennisver
@dennisver
Mar 01 2016 19:07
I believe my API doesn’t have to know the front-end needs
But I’l figure out something
I guess
haha