Tag: mongoose

使用聚合对嵌套列进行sorting

我有以下查询使用聚合框架在Mongoose中: Comment.aggregate([{ $match: { isActive: true } }, { $group: { _id: { year: { $year: "$creationDate" }, month: { $month: "$creationDate" }, day: { $dayOfMonth: "$creationDate" } }, comments: { $push: { comment: "$comment", username: "$username", creationDate: "$creationDate", } } } }, { $sort: { 'comments.creationDate': -1 } }, { $limit: 40 }], function […]

将由mongoose返回的数据转换为json

我正在用routes / index.js打印我的整个集合 exports.create = function(req,res){ new checklist_model({ name : req.body.name, comment : req.body.comment }).save(function(err,checklist_model){ res.redirect('/'); }); }; exports.checklist = function ( req, res ){ checklist_model.find({},function ( err,checklist_data,count ){ res.render( 'checklist', { title : 'checklist', checklist_data : JSON.stringify(checklist_data) });console.log(checklist_data); }); }; 我以下面的格式获取收集数据: { name: 'Stats', comment: 'Functionality test', _id: 53c5f7cdb79a40232193c45e, __v: 0 } 我如何得到这个以JSON格式打印? { […]

测验网站的Mongo模式

我正在构build一个小型的Node / Mongo应用程序,每天为用户提供最多3个问题。 用户只能回答是或否,正确的答案将在以后确定(这些问题更接近预测)。 目前,我有这些文件: 用户 ID 题 ID 问题答案 ID question_id(ref) UserAnswer ID question_id(ref) user_id(ref) 什么是最有效的方式来查询分贝,所以我得到今天的问题,但也检查是否该用户已经回答了这个问题已经? 我觉得我正在超越它。 我已经尝试了一些似乎是矫枉过正的方法。

mongoose自定义validation失败

在我的应用程序中,我试图在mongoose上运行一些自定义的validation,我想要的只是能够确保来自特定用户的评级不应超过一次,我已经尝试了几件事情,并且开始了代码正确返回true和false,但错误不会被触发。 这是我的代码 RatingSchema.path('email').validate(function (email) { var Rating = mongoose.model('Rating'); //console.log('i am being validated') //console.log('stuff: ' + this.email+ this.item) Rating.count({email: this.email, item: this.item},function(err,count){ if(err){ console.log(err); } else{ if(count===0){ //console.log(count) return true; } else { //console.log('Count: in else(failing)'+ count) return false; } } }); },'Item has been already rated by you')

如何检查模式已经在mongo数据库中创build?

我是mongo Db的新手,需要查看是否已经创build了模式。 我正在使用mongoose和node.js。 如果没有创build,我需要以某种方式运行创build脚本一次,否则继续与其他的东西。 TIA

如何以mongooserecursion的方式获取模型中的所有对象

想象这两个嵌套的mongoose模型,一个包含候选人列表的投票 var candidateSchema = new Schema({ name: String }); var voteSchema = new Schema({ candidates: [{ type: Schema.Types.ObjectId, ref: 'Candidate' }] }); voteSchema.methods.addCandidate = function addCandidate(newCandidate, callback) { this.candidates.addToSet(newCandidate); this.save(callback); }; var Vote = mongoose.model('Vote', voteSchema); var vote = new Vote(); var Candidate = mongoose.model('Candidate', candidateSchema); var candidate = new Candidate({ name: 'Guillaume Vincent' }); […]

为MongoDB查询duong,但允许某些基于时间戳的重复项

所以我有一组数据与时间戳相关联。 我想让mongo在3分钟的时间戳内重复那些重复的。 我会告诉你一个我的意思的例子: 原始数据: [{"fruit" : "apple", "timestamp": "2014-07-17T06:45:18Z"}, {"fruit" : "apple", "timestamp": "2014-07-17T06:47:18Z"}, {"fruit" : "apple", "timestamp": "2014-07-17T06:55:18Z"}] 查询后,会是: [{"fruit" : "apple", "timestamp": "2014-07-17T06:45:18Z"}, {"fruit" : "apple", "timestamp": "2014-07-17T06:55:18Z"}] 因为第二个条目是在第一个条目创build的3分钟内。 我已经得到了代码,以便聚合和删除具有相同水果的模糊,但现在我只想结合在时间戳泡沫内的模糊。

有没有办法创build一个“存储过程”调用多个查询在单个查询在mongodb(mongoose)

有没有办法在MongoDB中创build“存储过程”? 例如一个查询一次访问mongodb但实际调用多个集合(使用mongoose调用mongodb) 我知道mongodb有serverside javascript 。 但是不同的是,我不认为服务器端函数能够调用集合。 例如,如果我有下面的例子。 1.例子一(关系集合) mycollection1.findOne({_id: id}, function(err, result1){ mycollection2.findOne({_id: result1.id}, function(err, result2){ mycollection3.findOne({_id: result2.id}, function(err, result3){ return {}; }); }); }); 2.例子二(非关系集合) mycollection1.findOne({_id: id}, function(err, result1){ mycollection2.findOne({}, function(err, result2){ mycollection3.findOne({}, function(err, result3){ var someDataToDisplayToUI = { data1 : result1, data2: result2, data3: result3 }; return someDataToDisplayToUI; }); }); }); 我正在使用express.js服务器的mongoose,目前mongoose三次以上的例子。 有没有办法打一次?

无法使用HTTP Put更新MongoDB集合中的对象?

我是AngularJS的新手,我试图更新我的MongoDB数据库。 我正在尝试更新我的集合中的对象时遇到问题。 以下是我试图这样做的尝试: //listviewFactory is already injected and returns an event object //I call $scope.put when clicking (ng-click) on a button $scope.event = listviewFactory.getEvent(); $scope.put = function(event){ var currentUser = {}; if($cookieStore.get('token')) { currentUser = User.get(); } event.attendees.push(currentUser); $http.post('/api/events/' + event._id, event).success(function(data){ for(var i = 0; i < event.attendees.length; i++){ console.log("Attendees: ", event.attendees[i]); } $location.path('/'); […]

如何通过引用对象获取所有的值?

我有以下模式。 var orgSchema = new Schema({ name : String, email : String, webiste : String }); var empSchema = new Schema({ name : String, email : String, role : String, org : { type: Schema.Types.ObjectId, ref: 'org'} }); var leaveSchema = new Schema({ emp : { type: Schema.Types.ObjectId, ref: 'emp'}, from : Date, to : […]