我是mongoose和mongodb的新手,我正在尝试在我的收藏中创建一个新文档。 我可以更新现有的或从集合中获取所有文档。 我需要根据集合创建一个新文档(我现在有一个集合)。 - createMoment函数。
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://url and pass');
var usersSchema = require('./users_schema').usersSchema;
mongoose.model('usersM', usersSchema);
var usersAction;
mongoose.connection.once('open',function(){
var Users = this.model('usersM');
var query = Users.find();
query.where('userId');
query.exec(function(err, docs){
usersAction = docs;
console.log("docs: " + usersAction);
return usersAction;
});
});
exports.getData = function(){
return usersAction;
};
exports.createMoment = function(_fn, _ln, _conc,_lan,_lat,cb){
var Users = mongoose.model('usersM');
console.log("createMoment Called");
Users.insertOne({
'leadRank':1,
'adminApproval':true,
'userFname':_fn,
'userLname':_ln,
"conclusion":_conc,
'tentCoor':{'latitude' : _lat, 'longitude' : _lan}
}, null, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the users collection.");
cb(result);
});
};
我的架构: 我还没有传递一些字段
var mongoose = require('mongoose');
var schema = mongoose.Schema;
var usersSchema = new schema({
campName: {type:String, required:true},
centerCoor: [{
latitude:{type:Number},
longitude:{type:Number}
}],
zoom: {type:String},
users: [{
leadRank: {type:Number},
adminApproval: Boolean,
userFname: {type:String, required:true},
userLname: {type:String, required:true},
conclusion: {type:String},
tentCoor: [{
latitude:{type:Number},
longitude:{type:Number}
}]
}]
}, {collection: 'camps'});
exports.usersSchema = usersSchema;