These are chat archives for Automattic/mongoose

31st
Jan 2018
Kev
@lineus
Jan 31 2018 01:17
@JKobyP I'm glad you got it sorted! I hadn't heard of async.waterfall, so I ended up learning something too :) now I have to look into the bufferCommands option, heh
Kev
@lineus
Jan 31 2018 01:57

@bybrunobarros I updated that gist with output of the two you values that I think you're comparing, I've never used the Array function in that way, so I wasn't sure what exactly it was going to do at first, but after looking at the docs, it seems like it returns an array of length 1. It's set to <1 empty item>

console.log(Array(1)) // [ <1 empty item> ]

instead of the undefined that you'd be getting from const Provider = new mongoose.Schema({ notifications: [{}] })

patrickcrypt
@patrickcrypt
Jan 31 2018 09:42
Trying to do a really basic find on my model using a long string but it isn't returning anything..
This doesn't work : Token.find({address: "0x627306090abab3a6e1400e9345bc60c78a8bef57"}, but this does work: Token.find({address: /0x627306090abab3a6e1400e9345bc60c78a8bef57/} .. This is how it's stored in database: "address":"0x627306090abab3a6e1400e9345bc60c78a8bef57". Any idea why the first one won't work?
patrickcrypt
@patrickcrypt
Jan 31 2018 09:58
The string is from an express route parameter that has been saved to a variable that has typeof string
Kev
@lineus
Jan 31 2018 09:58
@patrickcrypt, are all of your token.address hex strings?
Kev
@lineus
Jan 31 2018 10:21
here is a gist that shows the query working for me with the string version.
Irfan Raza
@irfaan_aa_twitter
Jan 31 2018 13:02
I came across a situation where I need to find count while putting some condition inside populate
This is what doc says
http://mongoosejs.com/docs/4.x/docs/populate.html
Section - Query conditions and other options
However I see this is working for find only.. I want same level of control for count
Kev
@lineus
Jan 31 2018 13:04
@irfaan_aa_twitter can you paste ( or link to ) some code that shows your query? I'll see if I can help.
Irfan Raza
@irfaan_aa_twitter
Jan 31 2018 13:04
sure..
Irfan Raza
@irfaan_aa_twitter
Jan 31 2018 13:10
var query = Question.find({
            isDeleted: false
        });
        query.populate({
            path: 'subject',
            match: { isActive: { $eq: true } },
            select: 'name',
        })
Irfan Raza
@irfaan_aa_twitter
Jan 31 2018 13:27
subject is one of the object in my Question collection
When I run this, I get all the documents but subject gets replaced to null whenever there is "isActive"=false
What I want is, wherever subject is false, I want to drop that complete document itself not just the populated fields
let me know if our favorite mongoose has any option to achieve this
With raw mongo query I can achieve upto some extent I beleive using aggregate and multiple projection and $cond
Kev
@lineus
Jan 31 2018 13:49
@irfaan_aa_twitter can you also show your schema
Kev
@lineus
Jan 31 2018 14:05
I think the where method might work for what you're asking.
Irfan Raza
@irfaan_aa_twitter
Jan 31 2018 14:22
var timestamp = require('mongoose-timestamp');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var questions = mongoose.Schema({
    subject: {
        type: ObjectId,
        ref: 'subjects'
    },
    questionType: {
        type: String,
        required: true
    },
    questionsText: {
        type: String,
        require: true
    },

    isActive: {
        type: Boolean,
        default: true
    },
    isDeleted: {
        type: Boolean,
        default: false
    },
});
questions.plugin(timestamp);
module.exports = mongoose.model('questions', questions);
Kev
@lineus
Jan 31 2018 14:22
@irfaan_aa_twitter here is a gist that shows the where command in action based on what I think you mean.
Irfan Raza
@irfaan_aa_twitter
Jan 31 2018 14:23
Thanks.. Checked the gist..
The problem is my subject is pointing to some other collection
and I want to get questions of all active subjects. This active flag of subject is stored on Subject collection
this is why I can't use where as the field where I want to put condition belongs to some other collection
Kev
@lineus
Jan 31 2018 14:25
I thought using populate without at least one other collection was weird :) I'll fix the gist and see if I can get it working.
Irfan Raza
@irfaan_aa_twitter
Jan 31 2018 14:26
yes please .. if you can help on it..
Kev
@lineus
Jan 31 2018 14:26
bare with me, I'm still new at this. I'll try though!
Irfan Raza
@irfaan_aa_twitter
Jan 31 2018 14:26
In fact I believe this is very common use case for everyone or may be my model is wrong.
Here is how I can do on mysql
select q._id, q.isActive,s.name from questions q left join subjects s on s._id=q.subject where s.isActive=true;
Kev
@lineus
Jan 31 2018 16:06
@irfaan_aa_twitter I would just use populate to get the subject into every question.subject, and then filter out the non-active subjects in the populate callback or exec. heres a gist of that working. It uses where to filter the questions and then offloads the inclusion/exclusion logic onto the populate exec call.
Kev
@lineus
Jan 31 2018 17:32
I am looking at aggregate too, or rather, I'll look at that next :)
Irfan Raza
@irfaan_aa_twitter
Jan 31 2018 18:11
let me have look
Okay. I had look on gist and fortunately this is what I am also doing at my end right now..
:)
But while I got to know about match condition of populate my expectation got increased from mongoose so thought to ask here if any particular solution is available for this use case..
Anyway... I will keep exploring and will share with others if I would found better option
Kev
@lineus
Jan 31 2018 18:14
yeah, let me know if you find anything. I'll do the same
patrickcrypt
@patrickcrypt
Jan 31 2018 22:54
@lineus Thanks for your time :D. I was still never able to figure it out :(. For some reason, I am able to get it to work with this string "0xf17f52151ebef6c7334fad080c5704d77216b732", but none of the others I have tried.
patrickcrypt
@patrickcrypt
Jan 31 2018 23:01
Oh, actually maybe just figured it out.. seems that for whatever reason all of the strings are being stored in lower case only in the mongo database