Tag: mongoose

无arraysmongoose的子模式

所以,我想知道,即使我明白你不能创build一个单独的子文档,我仍然想创build一个子文档,以便我可以正确使用默认和其他mongoosetypes,有没有办法仍然这样做一个东西? 例如 : var SomeOtherScheme = new Schema({ a : { type:String, default:'test' }, b : { type:Boolean, default:false } … }); var GroupSettings = new Schema({ x : { type:Number, default:20 }, y : { type:Boolean, default:false }, … else : { type:SomeOtherScheme, default:SomeOtherScheme } }); var Group = new Schema({ name : { […]

Mongoose + Node.js:asynchronous问题

我在ReadyForReview集合中有一堆configuration文件。 每个configuration文件都包含一个“user_id_to_review”字段。 我想使用user_id_to_review将Users集合中的用户信息附加到每个configuration文件。 // looking for all ReadyForReview profiles ReadyForReview.find() .exec(function(err, profilesReadyForReview) { var profilesReadyForReviewArray = [] //array that will be populated with profiles and user info // for each profile, figure out what the info for the user is from user table for (var i = 0; i < profilesReadyForReview.length; i++) { var thisUserProfile […]

如何在express / mongoose中创build访问控制

我有expressjs和Mongoose(有mongodb)的项目。 我有多个用户angular色 – pipe理员,经理,用户 我希望每个表的几个字段可以被pipe理员访问,而其他的则不可以。 默认情况下用户不会有任何编辑访问权限,pipe理员将具有完全访问权限。 一种方法是通过查看用户的angular色来在每个控制器function中进行这种控制。 有没有简单的方法来做,如检查用户是否有控制,然后为每个表保存一次,以避免业务逻辑的重复。 我的ordersschema如下。 我不希望没有pipe理员权限的客户信息。 var OrderSchema = new Schema({ order_type: { type: String, trim: true }, customer_phone: { type: String, trim: true } }); var managerAccess = [order_type]; 我的用户模式如下(更多字段没有添加) var UserSchema = new Schema({ firstName: { type: String, trim: true, default: '', validate: [validateLocalStrategyProperty, 'Please fill in your first […]

mongoose填充其他语法

我有两个这样的模型: var UserSchema = mongoose.Schema({ name : {type: String, required: true}, image : {type: String, required: true} }); var RoomSchema = mongoose.Schema({ _user : {type: mongoose.Schema.ObjectId, ref: 'User', required: true}, name : {type: String, required: true} }); 我知道如何使用像这样的填充: Room.findOne({_id : req.body._id}).populate('_user').exec(function(err, room){ console.log(room); }); 工作正常,我的问题是,我需要写一些像( 女巫是错的 ): Room.findOne({_id : req.body._id}, function(err, room){ room.populate('_user'); console.log(room); }); […]

mongoldb更新()将无法正常工作

首先我创build了mongoose模式: var cardSchema = mongoose.Schema({ userId: String, imageUrl: String }) 然后我定义了模型: var Card = mongoose.model('Card', cardSchema) 然后我创build了一个新的卡: var newCard = new Card({ userId: "bablaba" }); 然后新卡被正确保存(我testing): newCard.save(function(err,theCard){ if(err) return console.log(err); console.log("saved"); console.log(theCard); }) 现在我需要更新卡中的信息。 我想添加/插入 imageUrl到保存的新卡。 以下是我尝试的几种方法: 1。 Card.where({ _id: _id }).update({ $set: {imageUrl:"blablab"} }); 2。 Card.update(_id,{$set:{imageUrl:"balbalab"}}) 3。 Card.update({_id: updateInfo._id}, {note0:updateInfo.note0, note1: updateInfo.note1},{multi:true}, function(err, numberAffected){}); […]

附加在Schemaless Array内

我是MongoDB的新手,到目前为止玩弄它,遇到了一个问题,在这里我试图在Schema-Less数组中添加多个对象时遇到困难。到目前为止,我尝试$ push追加数组内的多个对象,但是,有一个Mongo错误 。 [MongoError: Can't use $push/$pushALL within non-array 我不知道为什么我得到这个错误,当使用$推数组 架构: EventTypeSchema = new Schema(){ type: String, eventID: { type: Schema.Types.ObjectId, ref: 'User' } } PersonSchema = new Schema(){ PersonID: { type: Schema.Types.ObjectId, ref: 'User' } Invitation: [ ] //Schema-less } 在控制器我有访问到EventType和人员模型控制器: exports.update = function(req,res){ var event = new EventType(); event.type = 'EVENT'; event.eventID = […]

在dropDatabase之后不执行唯一的字段约束

我有一个Node / Mongoose / MongoDB项目,我使用Selenium WebDriver进行集成testing。 我的testing用例设置的一部分是在每次testing之前擦拭数据库。 我通过使用在这里被大量接受的方法通过命令行来完成此操作删除MongoDB数据库中的所有内容 mongo [database] –eval "db.dropDatabase();" 然而问题在于,运行这个命令之后,Mongo不再强制任何唯一的字段,比如我的Mongoose Schema定义的以下内容: new Mongoose.Schema name : type : String required : true unique : true 如果我在dropDatabase之后重新启动mongod ,那么再次强制执行唯一约束。 但是因为我的mongod是由一个单独的进程运行的,所以重启它作为我的testing用例的一部分会很麻烦,我希望不必要。 这是一个错误还是我错过了什么?

平行mongoose与promises /蓝鸟查询?

我有一个使用mongoose的nodejs / mongodb项目,还有蓝鸟的承诺。 这是我的问题:我有一个函数,有时需要执行1个查询,有时2个并行查询,然后join两个查询的结果。 这里是一个伪代码(因为我使用咖啡文本,但是这是不相关的): function(param) { // 1st query query1.exec // 2nd query, should be done in parallel with the 1st if (param) query2.exec // process requiring result from both queries callback() } 我知道如何执行asynchronous并行任务,但我不明白如何执行与蓝鸟或mongoose承诺。 谢谢,

在MongoDB中使用$ addtoset添加元素到数组

我正在尝试使用addtoset更新集合内的数组的示例。 新的元素正在被添加,但并非如预期的那样。 根据addtoset,只有当它不在列表中时,才会添加一个新的元素。 问题: 它只是采取任何元素被添加。 这是我的代码示例 架构(mongo_database.js): var category = new Schema({ Category_Name: { type: String, required: true}, //SubCategories: [{}] Category_Type: { type: String}, Sub_Categories: [{Sub_Category_Name: String, UpdatedOn: { type:Date, default:Date.now} }], CreatedDate: { type:Date, default: Date.now}, UpdatedOn: {type: Date, default: Date.now} }); service.js exports.addCategory = function (req, res){ //console.log(req.body); var category_name = req.body.category_name; var […]

用快递删除一个mongoose条目

我是整个平均堆栈的新手。 我即将创build一个CRUD应用程序。 我已经有了简单的,C和R,但现在我想去D.不幸的是,当我试图删除一个条目,我得到一个500错误。 我的路线正在调用,但之后没有任何反应。 HTML <div ng-repeat="article in articles"> <form ng-click="deleteArticle(article)"> <button type="submit" class="btn btn-primary">Delete</button> </form> </div> angular 在我的控制器中的部分.. $scope.deleteArticle = function(article) { articlesFactory.removeArticle(article) } 而在工厂里被召唤的部分: art.removeArticle = function(article) { return $http.put('/articles/' + article._id + '/remove') }; 路线 … router.param('article', function(req, res, next, id) { var query = Article.findById(id); query.exec(function (err, article) { if (err) […]