Tag: mongodb query

如何从mongoose模式中的数组中select一个值

使用MongoDb版本3.2.7 我有一个这样的mongoose模式,它有一个分支模式中的股票数组。 我想select股票数组中的一个subCategoryId ,并获得该subCategoryId currentPrice只,因为我正在查询 var getCriteria = { "stock.subCategoryId": subCategoryId } var update = { "stock.$.currentPrice": currentPrice } 此查询适用于更新值的时间,因为我可以完美地更新所选子subCategoryId的currentPrice 。 但是,当我想要得到一个特定的subCategoryId的价格下面的查询得到我在分支集合中的所有股票。 我正在使用的查询是这样的: var getCriteria ={ "stock.subCategoryId": subCategoryId } mongoose纲要 var branch = new Schema({ branchName: { type: String, trim: true, index: true, default: null }, isBlocked: { type: Boolean, default: false, required: true }, […]

MongoDB使用聚合$查找join两个集合,或者在Node.js中填充

这里有两个集合的架构 var activitySchema = new Schema({ activity_id: {type: String, index: {unique: true}, required: true}, begin_date : String, … }) var registrationSchema = new Schema({ activity_id: {type: String, index: {unique: true}, required: true}, registration_id: {type:String, trim: true, index: true ,required: true }, email : String, … }) 我想在同一个查询中的activity.begin_date , registration.id , registration.email 。 我能怎么做? 我在网上find了一些解决scheme,但是仍然不知道是使用填充还是聚合$lookup (这个看起来是新的)。 […]

如何在nodejs中将variables名称作为mongo db的集合名称

我正在使用节点js和mongo数据库,我想传递一个variables到集合 var id = "someid"; db.collection(id).insert("some json data"); 如果我这样做,它给我一个错误,因为集合名称必须是一个string。

在$ project阶段中重塑文档

对于我们的collections,看起来像这样: > db.companies.find().limit(1).pretty() { "_id": ObjectId("52cdef7c4bab8bd675297d8b"), "name": "AdventNet", "permalink": "abc3", "crunchbase_url": "http://www.crunchbase.com/company/adventnet", "homepage_url": "http://adventnet.com", "blog_url": "", "blog_feed_url": "", "twitter_username": "manageengine", "category_code": "enterprise", "number_of_employees": 600, "founded_year": 1996, "deadpooled_year": 2, "tag_list": "", "alias_list": "Zoho ManageEngine ", "email_address": "pr@adventnet.com", "phone_number": "925-924-9500", "description": "Server Management Software", "created_at": ISODate("2007-05-25T19:24:22Z"), "updated_at": "Wed Oct 31 18:26:09 UTC 2012", "overview": "<p>AdventNet is now […]

mongoose findByIdAndUpdate只更新文档中的一个字段

我想增加upvotes当用户点击一些button,还存储他的stormpath用户ID(req.user.href)在一个数组中。 尝试1: if (newUpvoters = found_post[0].upvoters.indexOf(req.user.href) == -1) { Problem.findByIdAndUpdate(found_post[0].id, { $push: { upvoters: req.user.href }} , {$inc: { upvotes : 1 } }, function (err, post) { if (err) return next(err); res.json(post); }) } 结果:只有upvoters数组被正确更新。 Upvotes不增加。 尝试2: Problem.findByIdAndUpdate(found_post[0].id, {$inc: { upvotes : 1 } }, { $push: { upvoters: req.user.href }}, function (err, post) […]

如何在mongodb中两次过滤数据

的NodeJS var filter = {}; filter.strBillDate = { "$gte": new Date(req.params.fromdate), "$lt": new Date(req.params.todate) }; Sales.find(filter).exec(function(err, salesdata) { return res.send(salesdata); }); 这里它会过滤这两天的数据。 我需要每天在这些时间之间过滤数据(即每周7PM到10Pm)

Node.js,mongodb和过滤的查询

我正在使用mongodb的本地API,并试图查询我的集合上的数据。 这是我的过滤对象: { email: 'admin@email.it', login: { '$exists': true } } 这是它应该find的一个文件: { "_id": "5829cd89a48a7813f0cc7429", "timestamp": "2016-11-14T14:43:18.705Z", "login": { "clientIPaddr": "::1", "clientProxy": "none" }, "userData": { "sessdata": { "sessionID": "CRTZaqpaUs-ep0J6rvYMBlQTdDakGwle", "email": "admin@email.it", "token": "3PlfQBVBoftlIpl-FizeCW5TbYMgcYTl4ZPTkHMVyxqv-TldWb_6U3eusJ27gtI64v7EqjT-KPlUUwkJK7hPnQ" } } } 但查询不会返回任何东西! 为什么?

MongoDBsearch匹配string部分的多个字段

这对我的同事MongoDB和NodeJs Devs来说是一个挑战 我有一个search表单,用户可以在其中input以下内容: 225/65R16 71T K715 Hankook 至于我的mongodb,我有一个收集与以下文档: 1- { _id: SomeId, Description: 225/65R16 71T K715, Brand: Hankook, Ref: 123455 } 2- { _id: SomeId, Description: 225/65R16 71T K715, Brand: Continental, Ref: 123456 } 3- { _id: SomeId, Description: 225/65R16 94T Energy, Brand: Hankook, Ref: 123457 } 我怎么能这样做,以便在search下面的任何组合时,我得到了上面的文档1和3的结果? 组合列表: Hankook 225/65R16 225/65R16 Hankook 225 Hankook […]

MongoDB / Mongoose – 按date查询对象数组

我有以下指标集合: { name: "Hello", values: [ { value: 2629, date: "2016-10-28T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee8" }, { value: 1568, date: "2016-10-29T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee7" }, { value: 1547, date: "2016-10-30T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee6" }, { value: 1497, date: "2016-10-31T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee5" }, { value: 3031, date: "2016-11-01T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee4" }, { value: 2559, date: "2016-11-02T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee3" }, { […]

如何通过条件匹配子文档中的数组元素

我有以下Mongoose模式: EmployeeSchema: var EmployeeSchema = new Schema({ name : String, employeeDetailsId: { type: Schema.Types.ObjectId, ref: 'employeedetails' } }); EmployeeDetailSchema: var EmployeeDetailSchema = new Schema({ employeeId: { type: Schema.Types.ObjectId, ref: 'employee' }, primarySkills: [ { type: Schema.Types.ObjectId, ref: 'skills' } ], }); SkillsSchema: var SkillsSchema = new Schema({ name: { type: String, required: true } }); […]