Tag: mongodb query

Mongoose查询:从Post Schema中填充前两个评论

我有3个集合:用户,发布和评论。 post有多个评论。 我想抓住50个职位,填充作者,填充评论,但我只想要按date(_id)sorting前2个最多投票评论 const PostSchema = new Schema({ author: { type: Schema.Types.ObjectId, ref: 'User' }, content: String, comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }] }); const Post = mongoose.model('Post', PostSchema); const CommentSchema = new Schema({ author: { type: Schema.Types.ObjectId, ref: 'User' }, content: String, votes: Number }); const Comment = mongoose.model('Comment', CommentSchema); Post .find({}) .limit(50) […]

select当月有生日的所有用户

我刚开始学习NodeJs + MongoDB(Mongoose)。 查询有问题。 我需要select当月有生日的所有用户 。 用户架构: const UserSchema = new mongoose.Schema({ email: { type: String, unique: true, required: true, index: true }, password: { type: String, required: true }, firstName: { type: String, required: true }, lastName: { type: String, required: true }, phone: { type: String, required: true }, birthday: { type: Date, […]

Nodejs + Mongodb:聚合后查找数据

我是Nodejs和MongoDB的新手。 以下是我的数据集示例: { 'name': ABC, 'age':24, 'gender':male, … } 一般来说,我想要做的就是在使用它们来查找不同的数据集群之前对数据进行聚合。 具体来说,我想知道有多less人在不同的年龄段。 然后,find每个年龄段的人(文件)并存储它们。 这是我的代码: MongoClient.connect(url, function(err, db) { if(err) { console.log('Unable to connect to the mongoDB server. Error:', err); } else { db.collection('test').aggregate( [ { $group: { _id: "$age" , total: { $sum: 1 } } }, { $sort: { total: -1 } } ]).toArray(function(err, result) […]

Express MongoDB find()基于_id字段

所以在我的快速应用程序,我试图find()基于我的_id字段。 看下面我的MongoDBlogging。 { "_id": { "$oid": "58c2a5bdf36d281631b3714a" }, "title": "EntertheBadJah", "subTitle": "Lorem ipsum dolor", "thmbNailImg": "", "headerImg": "", … BLAH BLAH BLAH 当我使用.find( _id: id ) ,我的id参数= 58c2a5bdf36d281631b3714a 。 我将如何转换/使用这个来获得我的MongoDBlogging? 这是我的快递电话: //GET ARTICLE app.get('/api/article', (req, res) => { var id = req.query.id var article = []; db.collection('articles') .find( _id: id ) .then(result => { article […]

Mongoose在聚合中使用子查询来总结数据

我是mongoose新手。 我有这样的模型。 var ForumSchema = new mongoose.Schema({ User_id : {type : Schema.Types.ObjectId, ref : 'User'}, Title : {type : String}, Content : {type : String}, Tags : [{type : String}], isPublic : {type : Boolean, default : false}, Vote : [{ User_id : {type : Schema.Types.ObjectId, ref : 'User'}, Kind : {type : Boolean} }], […]

按类别和date按date分组数据

我需要执行以下操作: 按date进行分组,然后logging总数和logging总数 按类别分组,然后聚合必须由所有类别的upvote总数和每天upvote和所有date的倒数投票所有类别的downvote。 这是我的代码: db.collection.aggregate([{ $unwind: '$votes' }, { $match: { 'category_id': array[i] } }, { $group: { _id:'$votes.date', "category_id": { $first: "$category_id" }, up_vote: { $sum: { $cond: [{ '$gt': ['$votes.score', 0] }, "$votes.score", 0] } }, down_vote: { $sum: { $cond: [{ '$lt': ['$votes.score', 0] }, "$votes.score", 0] } } } }, { […]

查找与顺序无关的嵌套数组的完全匹配

假设这些数据: { "_id" : ObjectId("594d43d1da7887921c701575"), "hashSets" : [ { "data1" : [ "2bf2cededea424f47d64d33c511dd431", "40499da7ee1456a2f79dc39a718aaf9b" ], "data2" : [ "b068931cc450442b63f5b3d276ea4297", "16ebd6af0cadece819da7468a9fd79f4", "8717cfca734e8987971f63b20eeb8024" ] }, { "data1" : [ "098F6BCD4621D373CADE4E832627B4F6", "5A105E8B9D40E1329780D62EA2265D8A", "AD0234829205B9033196BA818F7A872B" ], "data2" : [ "8AD8757BAA8564DC136C1E07507F4A98" ] } ], "__v" : 0 } 我正在尝试查询“hashSets”上的完全匹配,但“hashSets”数组中对象的顺序可能不同,并且数据数组中哈希的顺序可能有所不同。 我发现这个post有点类似: MongoDB查找精确数组匹配,但顺序无关紧要 虽然使用$ size和$ all是有道理的,但我不太清楚如何将它应用于嵌套的数组/对象。 我正在使用这个简单的查询,但似乎命令必须是相同的: db.analysis.find({hashSets:input.hashSets}) 编辑:为了清晰起见: input.hashSets = [ { […]

MongoDB $ cmp(聚合)给出错误的结果

我正在使用mongo的cmp聚合,并得到它的工作,但我认为我遇到的问题是一个错误。 当我比较10(或更大)说8.3时,返回-1。 当我比较9.5到8.3时,返回1。 好像我把十分之一或以上的数字与一个数字相比,我得到一个-1。 如果我比较一个人的工作正常。 以下是比较和结果的部分 cmpPrice: { $cmp: ['$amount', '$ticker.price_usd'] }, case "price": if (doc.cmpPrice == -1 && doc.direction == "above") { console.log("\t\tConditions Met, Price Above" ); emailUser(doc); } else if (doc.cmpPrice == 1 && doc.direction == "below") { console.log("\t\tConditions Met, Price Below"); emailUser(doc); } else { console.log("\t\tFailed"); } break; 结果 – ID: 59766722b006792254355adb […]

比较对象数组的查询

我想知道如何在Mongoose中比较(嵌套)对象的数组。 考虑到下面的数据,我想获得name属性匹配的结果。 任何人都可以帮助我吗? Organisation.find( { $or: [ { "category_list": { $in: cat_list } }, { "place_topics.data": { $in: place_tops } } ] } ) 假设这是存储在我的MongoDB中的数据: "category_list": [ { "id": "197750126917541", "name": "Pool & Billiard Hall" }, { "id": "197871390225897", "name": "Cafe" }, { "id": "218693881483234", "name": "Pub" } ], "place_topics": { "data": [ { "name": […]

来自node.js的MongoDB查询(express-coffeescript-mongodb)

我是新来的数据库查询与noSQL数据库,并有一个快速的问题。 本质上,我想检索所有具有“publishedImage”设置为“true”的字段的“名称”。 我想将结果存储在一个数组中,然后使用类似于while循环的方式将无序列表中的名称打印到视图文件夹中的.jade模板文件中。 我需要做什么查询? 我知道如何用php和mysql来完成这个任务,但是在查看完节点教程之后,我开始更加困惑。 如果我可以得到一些帮助,我将非常感激! 多谢你们, 乔尔。