-
Notifications
You must be signed in to change notification settings - Fork 11
Calling update(...) on a collection/model not working #14
Comments
All simpledb does is parse your model files and then stuff them into mongoose the way mongoose likes them done. I just didn't like the way that mongoose makes you create schemas and then register them as models and blah blah blah. I found myself constantly building little auto-loader scripts similar to what simpledb offers. Eventually I realized what I was doing over and over could be a simple wrapper module, thus simpledb was born. With that in mind, once your models are loaded onto the So I don't think this is a simpledb issue, but I'd be happy to help troubleshoot. I will try to run a similar query on a test database when I can and I'll update here with any new information. |
Any help appreciated! Maybe the problem is that db.User refers to the model, and I need to make the call on the collection? If so, can I access the collection for the db.User model? |
Yes, progress! calling |
Would be very helpful if your documentation mentioned collections, how they're not the same as Models and how to make calls on them... |
Again, anything after the This is literally all simpledb really does: // myModels/User.js
// Declare a simple node module that exports a schema property.
exports.schema = {
username: String,
joinDate: { type: Date, default: Date.now() }
}; // app.js
var fs = require('fs');
var path = require('path');
var mongoose = require('mongoose');
// Create db object.
var db = {};
// Open a mongoose connection and store the connection object on our new db object.
db.connection = mongoose.createConnection(mongooseConnectionString);
// When the open event fires, run the following logic.
db.connection.once('open', function () {
// Read all files from the myModels directory.
fs.readdir('myModels', function (err, files) {
if (err) return console.error(err);
// Iterate over all files in the myModels directory and load all the schemas into
// mongoose.
files.forEach(function (file) {
// Use the file name as the model name.
var modelName = path.basename(file.replace(path.extname(file), ''));
// Require the node module so we can get reference to the schema property.
var modelData = require(path.join('myModels', file));
// Create the mongoose schema using the schema data from the loaded module.
var schema = new mongoose.Schema(modelData.schema);
// Create a camel-cased version of our model name and use that as the property
// name on our db object.
var dbPropertyName = modelName.charAt(0).toUpperCase() + modelName.slice(1);
db[dbPropertyName] = db.connection.model(modelName, schema);
});
});
}); I just wrote the above code in so many projects that I ended up turning it into this simpledb module. There is a little more to it than that because simpledb lets you define |
PS - I believe the |
I have a user.js schema file that result in a db.User model. Now I want to remove a field called 'email_verified' from all User documents, like this:
But this does nothing. No change. Calling this in the mongo console result in an error, becuase the collection is really called 'users'. calling db.users.update(...) in the console works fine, but result in an error when called from my node.js code (as expected).
So is simpledb preventing the db.User.update(...) call in my node code from working?
The text was updated successfully, but these errors were encountered: