Tag: mongodb query

在总输出中组合不同的分组总计

现在我已经在$ project,aggregate()和$ group这个周末敲了一下头,现在是时候让我再次投入到你的仁慈之中。 我正在尝试拨打电话,回复用户的总数,按性别分组(这是比较容易的部分),并根据年龄段进行分组(这是击败了我)。 我得到它与一个小组合作: Person.aggregate([ { $match: { user_id: id } }, { $group: { _id: '$gender', total: { $sum: 1 } } } ]) .exec(function(err, result) { etc… 从那以后,它会给我多less男人,有多less女人在一个不错的输出中。 但是,如果我join第二组,似乎跳过第一个,扔第二组: Person.aggregate([ { $match: { user_id: id } }, { $group: { _id: '$gender', total: { $sum: 1 } }, $group: { _id: '$age', […]

根据MongoDB检查login凭据不起作用

router.post('/checkuser', function(req, res) { var db = req.db; var userEmail = req.body.useremail; var password = req.body.password; var collection = db.get('usercollection'); collection.find( { "email": userEmail }, function (err, doc) { if (err || !doc) { res.redirect("login"); } else { res.redirect("userlist"); } }); }); 这段代码应该检查MongoDB中的login凭据,如果值不匹配,则返回false 。 但是它总是redirect到userlist.jade文件。 有人可以解释为什么吗?

插入多个logging时删除相同的数据

喜ia有像50m +巨大的行,其中一些是重复的值 如果存在完全相同的键和值,我不希望mongo驱动程序添加它们 一个示例数据 [{ num: "+905081472117", name: "sulocan" },{ num: "+905351224578", name: "ahmet" },{ num: "+905251223578", name: "ahmet" },{ num: "+905081472117", name: "differ" },{ num: "+905081472117", name: "sulocan" }] 我不希望它只是检查数量我想它可以添加相同的数字与差异名称,但不添加相同的数字相同的名称 作为示例数据,我给我只希望它不会添加数组的最后一个值 exports.addnumber = function(num, cb) { db.collection('numbers', function(err, collection) { collection.insert(num, {safe:true}, function(err, result) { if (err) { res({'error':'An error has occurred'}); } else […]

如何在单个mongo查询中将字段的值分配给另一个字段

假设你有一个mongologging: { _id : 1234, x: 12, y: 34 } 我需要一个查询,以这样的方式更新一个特定的_id x = y;//assign the value of y to x y = new Date().getTime();//assign current time to y 对于特定的_id.current.currentCurrently我正在读取修改实体和更新它的id。这是可能做到这一点在一个单一的查询? 使用mongoose会是什么样子?

select字段的值作为数组mongoose / mongodb聚合

我有一个mongo查询 db.memberships.aggregate([{ '$match': { unit: new ObjectId('566fbaa1e63225b10bacac44') } }, { '$lookup': { from: 'seasons', localField: 'season', foreignField: '_id', as: 'season' } }, { '$unwind': '$season' }, { '$project': { season: { 'name': '$season.name', 'id': '$season._id' } } }, { '$group': { _id: '$_id', 'season': { '$addToSet': '$season' } } }, ]); 哪个结果 [ { "_id": […]

将一个数组作为子文档添加到Mongoose模型实例中

我正在构build一个数据的数组,并希望将该数组推到子文档。 var pubArray = []; var count = 5 for (i = 0; i < count; i++) { pubArray.push({publicationName: req.body.publicationName[i], dateSent:req.body.dateSent[i]}); }; Students.findOne({studentNumber: filter}, function (err, student) { student.publications.push({pubArray}); student.save(); }); 如果我在{publicationName: req.body.publicationName[i], dateSent:req.body.dateSent[i]}使用{publicationName: req.body.publicationName[i], dateSent:req.body.dateSent[i]} ,那么它工作正常。 如果我尝试推动arrays,没有任何反应。

延迟响应,直到所有查询完成

我的数据库包含项目和阶段。 项目可以有多个阶段。 模型类似于这些: 相: var phaseSchema = new mongoose.Schema({ project: { type: mongoose.Schema.Types.ObjectId, ref: 'Project' } }); 项目: var projectSchema = new mongoose.Schema({ name : { type: String } }); 目前我正在使用以下方法来检索每个项目的阶段: var calls = []; var projects = _.each(projects, function (p) { calls.push(function (callback) { req.app.db.models.Phase.find({ project: p._id }, function (err, doc) { if (err) […]

$和sub $,而不会产生mongo和mongoose的预期结果

modelThreader.find({ $and: [ {"date_entered": { $lt: fromTime }}, {"cancelled": { $ne: true }}, {"hideFromUserIds.id": { $ne: current_user_id }}, {"banned": false}, {$and: [ {"user_id": { $ne: current_user_id }}, {"privacy": { $ne: 1 }} ]} ] }).exec(function( err, docs ){ // handle response. }); 我在mongo中遇到了一个看似简单的查询的麻烦。 上面的查询我需要在查询中设置所有的第一批,即: {"date_entered": { $lt: fromTime }}, {"cancelled": { $ne: true }}, {"hideFromUserIds.id": […]

无法在查询中查找使用variables

我在NodeJS上的窗口上工作。 我使用下面的代码来检查login凭据。 它工作正常,直到我改变到Ubuntu 16.04 。 我build立了我的环境,现在variables不被视为string。 它从数据库返回空集。 如果我这样做,这个user_name:"some user name",password:"some password" ,它工作正常。 请帮忙。 我被卡住了:-(抱歉,如果它是一个noobtypes的错误:-) app.post('/login', function(req, res, next) { var N1 = req.body.N1; var N2 = req.body.N2; console.log(N1+N2);//Getting passed successfully MongoClient.connect(url, function(err, db) { if (err) { throw err; console.log("into connectivity error"); } console.log(N1); db.collection('col.login').find({user_name:N1,pass:N2}).toArray(function(err, result) { if (err) { throw err; } resultArray=result; try{ […]

在Nodejs中将值推入Mongodb模型

您好我正在使用MEAN stack与两个相互关联的数据模型: 发表和评论 所以在我的PostSchema中 comments:{ type:mongoose.Schema.Types.ObjectId, ref:'Comment' 并在我的CommentSchema有 post:{ type:mongoose.Schema.Types.ObjectId, ref:'Post' } 在我的视图中input注释之后,我希望后端将注释保存在Comment集合中,并将注释保存在Post集合中。 在我的服务器app.js文件中: comment.save(function(err,comment){ if(err){return next(err);} var post = new Post(req.post) post.comments.push(comment) // OR req.post.comments.push(comment);etc post.save(function(err,post){ if(err){return next(err);} res.json(comment); }) }) 但是,在我使用post.comments.push或req.post.comments.push ,我在命令行上得到一个错误消息, push is not a function 。 以上代码来自在线教程[1]。 我search了networking,但无法find任何类似的使用push例子。 你能不能让我知道我是否被误导了,如果有另一种方式我应该这样做? [1] https://thinkster.io/mean-stack-tutorial#wiring-everything-up