These are chat archives for Automattic/mongoose

2nd
Oct 2017
Arye Shalev
@pantchox
Oct 02 2017 00:01
Hi, quick question about 'refs' and 'populate', i have 2 collection lets say 'bookLists' and 'books' . 'bookList' has a schema of ['books'] with a ref. so when i create a new book i assign to its property 'bookListId' and also in bookLists i have the array of books refs which i use addtoset. now lets say i do an update to the book to be moved to a different list, so i have to update the book property of bookListId then remove the Ref in the original bookList it is associated to and then update the new bookList with a new Ref. this sounds like A LOT of work, is there a better way am i missing something? i am struggling with this for over 2 days(!) , looking back for your advise! thanks
Nick Rameau
@R4meau
Oct 02 2017 00:04
@pantchox I would personally just put a ref to the booklist on the book schema.
And just change the ID of the booklist in the book document when I want to move it to another booklist.
Arye Shalev
@pantchox
Oct 02 2017 00:06
@R4meau then how will i query it? i have api/booklists/ and i wanted to receive them "embedded" (not mongoDB embed subdocument)
Nick Rameau
@R4meau
Oct 02 2017 00:06
Then you look for every book that has the id of the booklist you want.
Arye Shalev
@pantchox
Oct 02 2017 00:07
i understand but that contradicts the point of refs and populate
Nick Rameau
@R4meau
Oct 02 2017 00:08
Maybe I didn't quite get your last question. Could you reformulate it?
Arye Shalev
@pantchox
Oct 02 2017 00:08
i just don't understand if my issue is by design of mongoDB or it is me having a bug in the code
what i mean is that i wanted to know if my initial question means that if that is what i want i am doing it right (as i mentioned a lot of work)
or with designing the schemas i am doing it the right way but i am WRONG about doing updates in 2 booklists
Nick Rameau
@R4meau
Oct 02 2017 00:10
I think it's not a good idea to put a ref on both schemas.
You should put a ref to the container (the booklist) instead of the content (the book).
Arye Shalev
@pantchox
Oct 02 2017 00:11
that is what i did but when updating a bookList document array of books, when i am updating the book to be in a different bookList document ref i need to update 3 documents, the book itself the old and new bookList
Nick Rameau
@R4meau
Oct 02 2017 00:13
If you only put a ref in the Book schema, when it comes to moving a Book document to another Booklist document, the only thing you need to change is the ID of the ref on that specific Book document. Nothing else.
Arye Shalev
@pantchox
Oct 02 2017 00:14
there is not point to put a "Ref" in the Schema, to the container, if eventually i am just querying all the bookLists and then - for Each bookList do a find on books where bookListId = x
Nick Rameau
@R4meau
Oct 02 2017 00:15
The goal of a ref is to be able to populate the container with the content. For example, you query for a specific book and you also want some information about the booklist, so you populate the booklist ref instead of querying the booklist too.
Arye Shalev
@pantchox
Oct 02 2017 00:16
so basically what you are saying is that i am doing a a simple query on the container - the booklist and on the booklist i have a field called ['books'] with Ref, it will actually do what i need?
Nick Rameau
@R4meau
Oct 02 2017 00:16
Nope.
I'm saying, do a simple query on the book (the content), and on the book, you have a ref to the booklist (the container).
Arye Shalev
@pantchox
Oct 02 2017 00:17
hmm i apperciate your help but i am a bit confused now :/
Nick Rameau
@R4meau
Oct 02 2017 00:17
Maybe someone else can hop in.
Arye Shalev
@pantchox
Oct 02 2017 00:17
but my goal is to query ALL the booklists and get 'populate' path of the books array for each booklist
Nick Rameau
@R4meau
Oct 02 2017 00:18
And I'm saying this is not a good idea. I think it's not scalable.
It's just my two cents.
Arye Shalev
@pantchox
Oct 02 2017 00:19
it should be a simple thing thats why i don't get it why its not working
thanks much apperciated! i will read our talk tommrow again too late here
if anyone read and can comment please do! i hope i am super wrong about my whole perspective on mongoDB
Dennis Belany
@azarus
Oct 02 2017 01:54
hm
where does it start?
@pantchox share code because the text is confusing
i really dont get what you're trying to achieve or why
Tom Beute
@beuteiful_twitter
Oct 02 2017 04:22
anyone here know how you should handle a schema that sometimes has an array of objects (sub schema), but sometimes its just an array of strings?
the only solution i've found is to use Schema.Types.Mixed but it seems like a last-resort
Nick Rameau
@R4meau
Oct 02 2017 04:24
I believe this is the right way to handle this case.
Tom Beute
@beuteiful_twitter
Oct 02 2017 04:24
with Mixed?
@R4meau
Nick Rameau
@R4meau
Oct 02 2017 04:25
Yes.
Or you could create separate fields, so it can be easy to maintain. Because if it's gonna be an array of sub schemas you might want to be able to validate new entries at a model level. Unless you take special care of this on the server.
Tom Beute
@beuteiful_twitter
Oct 02 2017 04:28
i just read about discriminators... might be a solution :clap:
Nick Rameau
@R4meau
Oct 02 2017 04:29
Are you sure discriminators are what you need? Doesn't sound like it to me. :/
Tom Beute
@beuteiful_twitter
Oct 02 2017 04:29
i'm uneasy about using mixed because it gives too much flexibility IMO
garbage data could end up in there
Nick Rameau
@R4meau
Oct 02 2017 04:30
Exactly, it's harder to maintain.
But you could validate the data on the server.
Before saving.
Tom Beute
@beuteiful_twitter
Oct 02 2017 04:30
separate fields as in, { fieldAsArr: [mySchema], fieldAsStr: [String] }?
Nick Rameau
@R4meau
Oct 02 2017 04:30
Yes.
Tom Beute
@beuteiful_twitter
Oct 02 2017 04:35
thanks @R4meau; im going to look more into this
Nick Rameau
@R4meau
Oct 02 2017 04:35
yw
Arye Shalev
@pantchox
Oct 02 2017 11:48
@azarus i am quoting myself - "Hi, quick question about 'refs' and 'populate', i have 2 collection lets say 'bookLists' and 'books' . 'bookList' has a schema of ['books'] with a ref. so when i create a new book i assign to its property 'bookListId' and also in bookLists i have the array of books refs which i use addtoset. now lets say i do an update to the book to be moved to a different list, so i have to update the book property of bookListId then remove the Ref in the original bookList it is associated to and then update the new bookList with a new Ref. this sounds like A LOT of work, is there a better way am i missing something? i am struggling with this for over 2 days(!) , looking back for your advise! thanks"
Dennis Belany
@azarus
Oct 02 2017 17:14
Arye shalev can u post code i meant to ask
Guys if anyone could help with this i'd really appriciate:
Arye Shalev
@pantchox
Oct 02 2017 18:01
@azarus will do when i get home thanks!
Arye Shalev
@pantchox
Oct 02 2017 18:37
general question: is it logical inside my model file where i have a method that returns the required fields of a document, that inside there i call another model with .find and eventually should return me the field from the result of the new query? because i receive the promise instead
Arye Shalev
@pantchox
Oct 02 2017 19:00
@azarus i have put these files to show what i am doing :
https://gist.github.com/pantchox/f1df06ca8db98e9cf30af9032da54bc4 - my 'list'
https://gist.github.com/pantchox/9bc332383ccfe640cb2982b0d5d33ef6 - my 'items'
https://gist.github.com/pantchox/be8023b6d3850de0dae48790e63abd4f - my query with populate
https://gist.github.com/pantchox/2bc6dac9f13602e7df937976696d9409 - my desired JSON result
I hope it makes clear what i am trying to acheive, this should be basic stuff and i don't understand what i am doing wrong.
The issue is that the books is not being populated under the bookLists and always return empty array :(
Dennis Belany
@azarus
Oct 02 2017 19:38
Yes
What are you tryinf to reference in the books?
bookListId
I think your query is ok, your array is empty because it cant find the items by its _id
Dennis Belany
@azarus
Oct 02 2017 19:43
I think you are using "id" to reference fields in the other collection
While it is not told mongoose what to use
and its looking for _id
Note the underscorw
E
@pantchox ping
In case you want to specify other fields than _id you have to tell mongoose
'field: 'capitalId' // use this field to get object id
Arye Shalev
@pantchox
Oct 02 2017 22:00
ok will try that!
@azarus i searched the docs for 'field' specification i could only find 'localField' and 'ForeignField'
Mu
@muloka
Oct 02 2017 22:05
@pantchox thanks for this i’m also having challenges with populate
From the docs it looks like as long as you store the ObjectId correctly that all that is needed is for calling populate and it’ll pull in the correct referenced document.
Whenever I do this I get null.
Arye Shalev
@pantchox
Oct 02 2017 22:13
@muloka i am struggling for this for like days, what i offered from the docs is not a "normal" case in my opnion
and as for storing ObjectId correctly what do you mean? mongoose do it for me automatic
Mu
@muloka
Oct 02 2017 22:27
ah seems i’m missing ref: my schema definition
Mu
@muloka
Oct 02 2017 22:36
got mine working… hope you figure yours out @pantchox
Arye Shalev
@pantchox
Oct 02 2017 22:37
thanks man
what are you working on?
Dennis Belany
@azarus
Oct 02 2017 23:08
@pantchox im back
can you confirm at least that your indexes are wrong?
Arye Shalev
@pantchox
Oct 02 2017 23:08
how come
Dennis Belany
@azarus
Oct 02 2017 23:09
i mean _id
not index
Arye Shalev
@pantchox
Oct 02 2017 23:09
_id is being generated by mongoose no?
Dennis Belany
@azarus
Oct 02 2017 23:09
yes.
Arye Shalev
@pantchox
Oct 02 2017 23:09
ok so how can i confirm that since i am not defining it on the Schema?
Dennis Belany
@azarus
Oct 02 2017 23:09
and your Books _id must be the same as what you put into your Ref field
check your database?
what data did you put into your book lists?
Arye Shalev
@pantchox
Oct 02 2017 23:10
bookListId: { type: Schema.ObjectId, ref: 'BookLists', required: true },
Dennis Belany
@azarus
Oct 02 2017 23:12
I have my user collection like this
"email" : "test@test.com",
"group" : ObjectId("5910ec1bc0ec9032c8bea82d"),
where group is referencing the usergroups colleciton
Arye Shalev
@pantchox
Oct 02 2017 23:13
i assume i just have the id itself
Dennis Belany
@azarus
Oct 02 2017 23:13
now
db.getCollection('usergroups').find({_id: ObjectId("5910ec1bc0ec9032c8bea82d")})
note that _id is the same
{
    "_id" : ObjectId("5910ec1bc0ec9032c8bea82d"),
    "name" : "Test",
    "groupType" : "Test",
    "acl" : [ 
        "App", 
    ],
    "allowRegistration" : false
}
Arye Shalev
@pantchox
Oct 02 2017 23:16
so when in my "controller" when i get the data from express with the "id" which is a string i need to create a new instance of ObjectId with that string and then put that instead?
when i say id i mean the "bookListId"