These are chat archives for Automattic/mongoose

25th
Jan 2017
Michael Leanos
@mleanos
Jan 25 2017 00:31
Has anyone used the new ES6 Class integration feature with TypeScript? I would love to see a working example. I've been wrestling with it for a couple hours now.
Paul "Joey" Clark
@joeytwiddle
Jan 25 2017 07:09

@tomspeak It looks to me like they could be non-unique, if the same choice document was pushed onto the choices array of multiple poll documents.

But that should never happen, right? So don’t worry about it.

And if it did ever happen, then I guess you have a bigger problem than just this query! So you might want to prevent it from happening somewhere else.

Each time a new choice document is created, its _id will be unique yes.

Just make sure you never add the same choice document to two polls.

If you ever clone a poll, make sure those _id don’t get cloned. I think you will get that behaviour from either poll.toObject() or from JSON.parse(JSON.stringify(poll)) but better check. ;)
Ankur sharma
@ankur1163
Jan 25 2017 08:00

can someone check this mongodb code

 if(rid){
                       console.log("restaurantid found")
                Yelpcontroller.update({$push: {
                        'usernames': username

                    }})
Yelpcontroller.findOne({ 'restaurantid' :  id }, function(err, rid) {
             if(err){
                 console.log("there is error from mongodb")
             }
             else{

                   if(rid){
                       console.log("restaurantid found")
                Yelpcontroller.update({$push: {
                        'usernames': username

                    }})
            }
            else{
                console.log("restaurant id not found")
                Yelpcontroller.create({
                "restaurantid": id,
                "usernames": [username]
            })

            }

             }

my else block work
but if block doesnt work
i want to update it and want to push username in usernames array
what am i doing wrong here?

var mongoose = require('mongoose');

var YelpSchema = new mongoose.Schema({
    restaurantid:{type:String,default:"noidgiven"},
    usernames:{type:Array,default:[]}

});

module.exports = mongoose.model('YelpSchema', YelpSchema);
thats my model
Andy Alameddine
@andyalam
Jan 25 2017 18:58
Does anyone have mongoosejs experience? I'm trying to grab all posts under a specific category.
const categorySchema = new mongoose.Schema({
  category: { type: String, required: true, unique: true },
  title: { type: String, required: true, unique: true }
});

const voteSchema = new mongoose.Schema({
  user: { type: String, required: true },
  positive: { type: Boolean, required: true }
});

const postSchema = new mongoose.Schema({
  user: { type: String, required: true },
  date: { type: Date, 'default': Date.now },
  blobbase64: { type: String, 'default': '' },
  description: { type: String, 'default': '' },
  votes: [voteSchema],
  category: {type: Schema.ObjectId, ref: 'Category'}
});



Post
    .find({ 'category.category': categoryType })
    .populate('category')
    .exec((err, posts)= { //do stuff with posts here }
Found it! This link: http://mongoosejs.com/docs/populate.html under "Query conditions and other options". Thanks for the rubber duck debugging lol
  Post
    .find({})
    .populate({
      path: 'category',
      match: { 'category': categoryType }
    })
There.
realisation
@realisation
Jan 25 2017 22:42
hi guys, I'm trying to rename a field like so, but nothing is being renamed, the new field is in the schema, too:
   Notification.update({},{$rename: { 'content': 'subject' } },
  {multi: true}, function(err, result){
    console.log("RESULT", result);
  });