These are chat archives for Automattic/mongoose

20th
Mar 2016
Max
@maxfi
Mar 20 2016 10:00
Hi all! Does anyone know if it's possible to ensureIndex on a collection for which no model or schema has been created? I'm thinking something along these lines:
var indexRequest = {
  collection: 'someCollection',
  indexes: { someField: 1}
};

mongoose.connect(dbUri, dbOptions).then(function() {
  console.info("MongoDB connected");
  // set index here ???
  // may be something like:
  // return mongoose.db[indexRequest.collection].ensureIndex(indexRequest.indexes);
}).then(function(result){
  console.info(result); // => ensureIndex op result
});
Max
@maxfi
Mar 20 2016 11:11
Figured it out: :smile:
var indexRequest = {
  collection: 'someCollection',
  indexes: { someField: 1}
};

mongoose.connect(dbUri, dbOptions).then(function() {
  console.info("MongoDB connected");
  return mongoose.connection.db.collection(indexRequest.collection).createIndex(indexRequest.indexes);
}).then(function(result){
  console.info(result); // => createIndex op result
});
J
@jhyland87
Mar 20 2016 15:27
Hey, quick question. I have some model methods that just fetch documents, if no records are found for whatever the search criteria is, should it return an error, or an empty array? (or null?)