Tag: mongoose

Mongoose model.save()不会持久更新数据库

我正在开发一个mongoose / node.js / express应用程序。 在我的路线我使用快速'app.param()方法来获取我的模型实例到请求中 – 而不是在每个控制器操作中获取它。 我得到了show并create在我的控制器工作的行动 – 但是我卡住实施update行动。 这里是我的相关控制器代码: // mymodel-controller.js var mongoose = require('mongoose') var MyModel = mongoose.model('MyModel'); var utils = require('../../lib/utils'); "use strict"; exports.update = function (req, res, next) { var mymodel = req.mymodel; mymodel.set(req.body); mymodel.slug = utils.convertToSlug(req.body.title); mymodel.save(function(err, doc) { if (!err) { console.log('update successful'); // here i see […]

MissingSchemaError为Mongoose的填充方法

我试图工作mongoose的填充,但不断得到这个错误: /node_modules/mongoose/lib/connection.js:625 throw new MongooseError.MissingSchemaError(name); ^ MissingSchemaError: Schema hasn't been registered for model "Users". 我的代码只是基本的填充function的实验。 它看起来像这样: 我定义了模式: var user = mongoose.Schema({ userName: String }); var client = mongoose.Schema({ fk_user: { type: mongoose.Schema.ObjectId, ref: 'Users' }, name: String }); 我创build模型: var UserModel = mongoose.createConnection('localhost','fixDB').model('Users', user); var ClientModel = mongoose.createConnection('localhost','fixDB').model('Clients', client); 我保存了一个实例: var user1 = new UserModel({userName: […]

mongoose人口忽略select

我很难理解mongoose的人口逻辑。 我有一个UserSchema其中包含文章的数组。 articles: { type: [{ article: { type: Schema.Types.ObjectId, ref: "Article" }, read: Boolean, starred: Boolean }] }, 文章存储在一个单独的集合。 现在,我想查找用户的所有已加星标的文章,填充这些文章,按发布date对它们进行sorting并对结果进行分页。 User.findOne({_id: user.id, "articles.starred": true}).populate({path: "articles.article", options: {sort: {"published": -1}, skip: 20, limit: 20}}).exec(function(err, _user) { callback(_user); }); 问题是,人口已经完成,但是选项完全被忽略了。 以下是用户和文章的外观: 用户: { "__v" : 0, "_id" : ObjectId("5284ef45fe46a55194000003"), "articles" : [ { "article" : ObjectId("5284ef52fe46a55194000005"), […]

mongoose无法使用$ in操作符find引用

我有这样的用户架构 var userSchema = new mongoose.Schema({ username: String, following: [{created: {type: Date, default: Date.now}, target: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }}] }); 和一个用户活动模式: var activity = new mongoose.Schema({ text: String, private: Boolean, _creator: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true } }); 我试图获得所有的活动,其中_creator是$in的following子文档$in 。 userModel.methods.getNewsfeed = function(callback){ var following_targets = _.pluck(this.following, 'target') // […]

重新格式化date正则expression式

我正在编写一个应用程序(Node.js是精确的),我现在有一个datestring格式化在mongoose数据库中: 2013-11-19T00:10:00-08:00 我想通过查找在给定的一天中发生的所有结果来在数据库上运行查询 它需要在所有的date工作,而不仅仅是上面的例子

PB mongodb,mongoose,node.js

当我尝试在mongodb中插入某些东西时,出现这个错误,您能否给我一个理解的文档,并做我需要的东西? /home/samp/Bureau/Node/gw/gw-user/node_modules/mongoose/lib/utils.js:413 throw err; ^ MongoError: E11000 duplicate key error index: mongoose-bcrypt-test.users.$username_1 dup key: { : "samp@hotmail.com" } 这是我的代码: user.save(function(err) { if (err) throw err;

使用node.js和mongoose填充另一个引用文档的数据

我正在构build一个运行课程的应用程序。 每门课程都有单位。 每个单元最后都有页面,问题和测验。 我以为我有一切正确的设置,但如果我查看单位列表,它只显示ID,并不填充。 楷模: var unitSchema = new Schema({ number: Number, title: String, unitCode: String, pageCount: Number, time: Number }); var pageSchema = new Schema({ number: Number, title: String, content: String, courseQ: Boolean, unitCode: String }); var courseQuestionSchema = new Schema({ question: String, choices: [], correct: Number }); var quizSchema = new Schema({ code: […]

是否可以在集合级别设置读取偏好?

我有一个大型的元数据集合,我想知道是否有一种方法来为整个集合设置secondaryPreferred读取首选项,而不是在整个应用程序代码的查询级别重复设置它。 这可能吗? 目前使用mongoose3.6.x.

mongoose – 如何指定和删除依赖关系?

使用MongoDB和Mongoose,如何将对象标记为从属于它们的关系,以便如果父对象被删除,则所有依赖于它的子对象也将被删除。 例如,如果作者被删除,我怎样才能让作者的所有文章自动删除? var AuthorSchema = new Schema({ created: { type: Date, default: Date.now }, name: { type: String, default: '', trim: true } }); var ArticleSchema = new Schema({ created: { type: Date, default: Date.now }, title: { type: String, default: '', trim: true }, content: { type: String, default: '', trim: true }, author: […]

使用Mocha和should.js进行NodeJStesting数据库

我用摩卡testing我的NodeJS应用程序,应该。 第一次testing顺利进行时,第二次失败(错误等于空)。 在这两个testing中,如果在callback中有一个有效的用户(两者在mongoose中都有相同的ID)。 testing显然不等待数据库操作发生。 describe("User", function(){ before(function (done) { // clear database model.UserModel.collection.remove(done); }) it("should save", function(done){ user1.save(function(error, user){ should.not.exist(error); user.should.have.property("first_name", "Rainer"); done(); }) }) it("should not save duplicate user", function(done){ user1.save(function(error, user){ should.exist(error); done(); }) }) }) 当我在第一个testing的callback中放置第二个testing时,它也不起作用。 我想testing一个重复的键错误,但不能达到给定的情况。