These are chat archives for Automattic/mongoose

14th
May 2017
Ankur Anand
@ankur-anand
May 14 2017 07:05
index: true vs unique: true what is the actual difference between these two in mongo and when to use index: true over unique: true in mongoose schema
https://docs.mongodb.com/manual/core/index-unique/
On this page all its written about "A unique index ensures that the indexed fields do not store duplicate values; i.e. enforces uniqueness for the indexed fields. By default, MongoDB creates a unique index on the _id field during the creation of a collection.
"
Not seeing anything about index: true anywhere
vivek sharma
@viveksharma1
May 14 2017 12:46
CA
Conway Hyacienth
@ZookeeMedia
May 14 2017 23:03

Hi people, I have a schema

var UserSchema = new mongoose.Schema({
  username: {type: String, lowercase: true, unique: true, required: [true, "can't be blank"], match: [/^[a-zA-Z0-9]+$/, 'is invalid'], index: true},
  email: {type: String, lowercase: true, unique: true, required: [true, "can't be blank"], match: [/\S+@\S+\.\S+/, 'is invalid'], index: true},
  bio: String,
  image: String,
  favorites: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Article' }],
  following: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
  hash: String,
  salt: String
}, {timestamps: true});

and I'm looking to output the array from the 'following' key. So far my efforts have only gleaned null values in the client. Yet the correct values appear int he console.log output.

router.get('/users/friends', auth.required, function(req, res, next) {
  var limit = 20;
  var offset = 0;

  if(typeof req.query.limit !== 'undefined'){
    limit = req.query.limit;
  }

  if(typeof req.query.offset !== 'undefined'){
    offset = req.query.offset;
  }

  User.findById(req.payload.id)
  .then(function(user){
    if (!user) { return res.sendStatus(401); }

      return res.json({
      users: user.following.map(function(username){

            User.findById(username)

            .then(function(userlist){
            console.log('userlist:',userlist.username);
            return userlist.username;

            })
            .catch(next)
      })
      })

  })
  .catch(next);

});