These are chat archives for Automattic/mongoose

3rd
Feb 2016
Seo ji woong
@Seojiwoong
Feb 03 2016 10:57
hi guys
hello? any body here?
Konstantin Baierer
@kba
Feb 03 2016 11:02
@Seojiwoong :wave:
Seo ji woong
@Seojiwoong
Feb 03 2016 11:13
hi kba
what mongoose version do you use?
Konstantin Baierer
@kba
Feb 03 2016 11:22
@Seojiwoong Version varies. But >= 4.1.10
Seo ji woong
@Seojiwoong
Feb 03 2016 11:25
@kba thanks.
J
@jhyland87
Feb 03 2016 18:52
Hey guys, is there a way to add an element to the documents returned from a query, without having to create a Virtual element ?
Heres what im trying (FYI, Im using lodash)
    assetSchema.statics.getAssets = function( partitionID ){
        return new Promise( ( res, rej ) => {
            Mongoose.models.Partition.getPartition( partitionID, true )
                .then( partitionData => {
                     // Doing some other stuff here... (Hence the nested query)
                    this.find( { _partition: partitionID } )
                        .then( assetData => {
                            res( _.mapValues( assetData, a => {
                                // Trying to add a 'foo' element
                                a.foo = 'bar'
                                return a
                            }) )
                        } )
                        .catch( err => rej( err ) )
                })
                .catch( err => rej( err ) )

            //this.find( { _partition: partitionID } ).populate( 'attributes._field' ).populate( '_partition' )
        })
    }
The line where im returning a mapped result of the query, where I add the foo to each object
I dont want to save that item to the document or anything
This message was deleted
J
@jhyland87
Feb 03 2016 18:59
Nvm, I see, its because its a mongoose document, If I use lean the above works fine
Sebastien Vaucouleur
@vaucouleur
Feb 03 2016 19:19
@jhyland87 Hi, what's wrong with virtuals ? Why do you want to avoid them ?
J
@jhyland87
Feb 03 2016 19:19
No, Id love to have this in a virtual, but it would need data I dont think I can access from a virtual
ill try to explain, lol
SchemaA references SchemaB, which has an item that references SchemaC (anarray). im not sure how I can access SchemaC from the virtual in SchemaA
Is that possible?
Id much rather have it as a virtual, so I was going to create a Stackoverflow question, asking how to do this, lol
Sebastien Vaucouleur
@vaucouleur
Feb 03 2016 19:25
there's no callback in the getter signature, as far as I know, so that might be an issue. But, anyhow I guess that you could create a setter and set the property to whatever you want
J
@jhyland87
Feb 03 2016 19:25
Im trying to type up a smaller example, heh, gimme a sec
Sebastien Vaucouleur
@vaucouleur
Feb 03 2016 19:28
(methods have callbacks)
J
@jhyland87
Feb 03 2016 19:28
    // models/modela.js

    // SchemaA for ModelA
    const SchemaA = new Schema({
        _foo: {
            // References ModelB._foo
            type: Schema.Types.ObjectId,
            ref: 'ModelB'
        }
    })

    SchemaA.virtual('something').get(function () {
        // How can I access ModelC._baz
        return 'Value from ModelC'
    });

    // models/modelb.js

    // SchemaB for ModelB
    const SchemaB = new Schema({
        _bar: [{
            // References ModelC._bar
            type: Schema.Types.ObjectId,
            ref: 'ModelC'
        }]
    })

    // models/modelc.js

    // SchemaC for ModelC
    const SchemaC = new Schema({
        _baz: Schema.Types.String
    })
Ok, so thats the contents of 2 files
SchemaA references something in SchemaB, which references something in SchemaC
Referencing them all is fine, but when I look at the data in the something virtual, it doesn't do the reference
I tried to reference it somehow in the query, thinking that would work, but I couldnt get it to work.. ModelA.find( ).populate( '_foo' ).populate( '_foo._bar' )
I never thought that would work, but I tried anyways, lol
I know I could accomplish this through a callback, but id rather have it as something I can just refer to and have it return a string, say I wanted to use the value it returns in a sentence, and I wanted to call the same virtual on 10 different documents.... that would be a pain in the ass to do via callbacks
@vaucouleur hope that explains
J
@jhyland87
Feb 03 2016 19:34
brb
Sebastien Vaucouleur
@vaucouleur
Feb 03 2016 19:34
@jhyland87 I see. I guess you already checked that the populates worked as expected ?
@jhyland87 it seems that you're trying to deep populate, then reference one of the deep collection element in one of the virtual
if someone has a perfect answer for this, I'll also be interested to know
Timothy Gu
@TimothyGu
Feb 03 2016 20:11
Hi all. I wonder if there is an easier way to declare the subtype of a Buffer in the schema itself
right now I'm setting the subtype whenever I need to create a Buffer, which is very inconvenient
J
@jhyland87
Feb 03 2016 21:12
@vaucouleur ill post something on StackOverflow
J
@jhyland87
Feb 03 2016 21:25
@vaucouleur SO Thread
antonioaltamura
@antonioaltamura
Feb 03 2016 23:17
guys it's 3 days now i'm stuck on the same problem :worried:
I already posted here: I just want to save a "post" with multiple authors (some authors is already in user collection, some could be new, if so I have to save them and then save the post). This is the code but obiouvsly I'm missing something about those callbacks, the array 'arr' printed is empty.
function findOrAdd(user){
    var newuser=null;
    var user=null;
    async.series([
            function(callback){
                com_user.findOne({_id: user._id},
                    function(err,obj) {
                        if(obj) {
                            console.log('user found');
                            users=obj
                            callback(null);
                        } else {
                            newuser=user.label
                            callback(null);
                        }
                    });
            },
            function(callback){
                if(newuser){
                var com_user1 = new com_user({
                    name: newuser,
                    age: 20
                });
                com_user1.save(function (err, n) {
                    if (err)
                        console.log(err);
                    else {
                       console.log('User not found and just added!');
                        user=n;
                        callback(null);
                    }
                });
                } else callback(null);
            }
        ],
        function(err, results){
            return user._id;
        });
}
app.post('/api/community/posts', function(req,res){
    var arr=[];
    var newusers=[];
    async.forEachOf(req.body.postedBy, function (user, key, callback) {
        arr.push(findOrAdd(user));
    }, function (err) {
        if (err) console.error(err.message);
        console.log(arr)
    });
   });