These are chat archives for Automattic/mongoose

10th
Jun 2016
Mohamed Nasrullah
@nasr18
Jun 10 2016 03:10
will return only one document or two??? @z424brave
Damian Kelly
@z424brave
Jun 10 2016 07:49
It will return 0 - n documents - i.e. any main document that has either of the 2 tag _id references in its "tags" attribute.
the main document has a "tags" attribute which is an array of Tag references i.e. defined as "tags: [{type:mongoose.Schema.Types.ObjectId, ref:'Tag'}]"
Mohamed Nasrullah
@nasr18
Jun 10 2016 11:37
is it possible to add mulitple ref inside a single field???
like,
createdBy: {
        type: mongoose.Schema.Types.ObjectID,
        ref: ['admin', 'user']
    }
Damian Kelly
@z424brave
Jun 10 2016 12:30
blob
Might need to click to see properly
tags contains an array of tag id's which are the id's of objects of collection Tag
its not multiple refs - its an array of the same ref
Mohamed Nasrullah
@nasr18
Jun 10 2016 12:33
mmm i know. @z424brave
are you answering my question?? @z424brave
Damian Kelly
@z424brave
Jun 10 2016 12:34
sorry I thought you were seeking clarification of my issue
I don't know the answer to your question
Mohamed Nasrullah
@nasr18
Jun 10 2016 12:34
its ok
Daniel Acuña
@moustacheful
Jun 10 2016 16:32
Hello. How can I query for a specific property in an array of subdocuments? I just want to check if a user belongs to a particular resource
// Model for Account.
//... rest of the definition
    users:[
        {
            role: {
                type: String,
                enum: ['admin','editor','analist'],
                default: 'analist'
            },
            user: {
                type: Schema.Types.ObjectId,
                ref: 'User',
                required: true
            }
        }
    ],
//...rest of the definition

// Query - this does not work 
let currentUser = yield User.find("id");
Account.query({
    users: {
        user: currentUser._id
    }
})

// Should return all the resources which contain currentUser in the users array.
LeonineKing1199
@LeonineKing1199
Jun 10 2016 16:33
I think you can use $elemMatch
Daniel Acuña
@moustacheful
Jun 10 2016 16:35
cool, that did the trick
thanks!
that was fast
LeonineKing1199
@LeonineKing1199
Jun 10 2016 16:36
Yeah, no problem. Btw, it looks like you're using co. Good job.
Daniel Acuña
@moustacheful
Jun 10 2016 16:36
yeah, using koa. really nice so far :)
Valeri Karpov
@vkarpov15
Jun 10 2016 19:54
@moustacheful yeah koa's pretty sweet. Not my favorite but it's got some great ideas :)
@nasr18 I think you're looking for dynamic references: http://mongoosejs.com/docs/populate.html#dynamic-ref
Speaking of awesome populate features, anybody up for helping me QA Automattic/mongoose#4219 ? Groundbreaking new populate API that's coming in 4.5.0
@Regnised that's because a mongoose document has a lot of internal state for dirty checking, etc. If you do doc.toObject() you'll get a POJO that has the fields you expect.
LeonineKing1199
@LeonineKing1199
Jun 10 2016 20:00
It's weird, sometimes I have to call toObject to make Mongoose models work with Ramda
Is that the fault of Ramda or is it an effect of Mongoose?