These are chat archives for Automattic/mongoose

5th
Mar 2017
Brandon Wozniewicz
@brandonwoz
Mar 05 2017 04:19

I am trying to seed a mongo database with users and the users friends.

Users is it's own Model (with a ref to the Friends model).

I have two arrays of data to use to seed the database. The friends array contains sub arrays. Each subarray –in order– contains the friends for the respective user array. i.e friends[0] are the friends for user[0].

The goal is to create the user, then search to determine if the users friends document exist all-ready. If they don't, create the friend document, and push them to that user.

The code creates the users, but only creates the friends from the friends[0]. It seems to do with async, but have not idea where to go next.

 function doSomething(){
        var i = 0;
        userArray.forEach(function(user){
            User.create(user, function(err, createdUser){
                if(err){
                    console.log(err);
                }
                console.log(createdUser.name + ' created!');
                var incrementer = 0;
                for(let friend of friends[i]){
                    Friend.find({name: friend.name}, function(foundFriend){
                        if(foundFriend == undefined){
                            Friend.create(friend, function(err, createdFriend){
                                createdUser.friends.push(createdFriend);
                                createdUser.save;
                                console.log(createdFriend.name + ' added to' + createdUser.name + ' friends!');
                                incrementer++;
                                if(incrementer === friends[i].length){
                                        i++; 
                                    }                                        
                            });
                        }
                    });
                }
            });
        });
    }

    //Output I would expect (assuming no documents exist)

    //John Smith created!
    //Sally added to John Smith friends
    //Mike added to John Smith friends

    //Mary Doe created!
    //Bob added to Mary Doe friends!
    //Gary added to Mary Doe friends!

    //Instead I am getting (assuming no matching documents exist)

    //John Smith created!
    //Mary Doe created!


    //Sally added to John Smith friends!
    //Sally added to Mary Doe friends!
    //Mike added to Mary Doe friends!
    //Sally added to Mary Doe friends!
rebello
@rebello
Mar 05 2017 08:41
Hi I need help : Mongo query to drop all records except top 50 records from a collection
dumptyd
@dumptyd
Mar 05 2017 08:46
Model.find().skip(50).remove().exec(function(err, data) {
    if(err)
        throw err;
    console.log('deleted');
}
This should work.
@rebello
rebello
@rebello
Mar 05 2017 08:53
Model.find().skip(50).remove() or delete()
dosent seems to work
when i do a count Model.find().count will return the same number of records
dumptyd
@dumptyd
Mar 05 2017 08:54
did you use exec or not?
rebello
@rebello
Mar 05 2017 08:55
i am doing it via mongo shell
dumptyd
@dumptyd
Mar 05 2017 08:56
Oh :| I don't know then. I always use mongoose
rebello
@rebello
Mar 05 2017 08:56
oh ok thanks