Tag: mongodb query

是否有可能更新查询中的集合中的值?

我不确定标题是否解释了我想说的。 但是这里假设集合的结构是这样的; [ {email: "alex@sda.com"}, {email: "elizabeth@sds.com"}, {email: "hannah@xx.com"}, ] 我想收到电子邮件,但在@字符之前。 我做了什么来做到这一点? db.collection.find({}).toArray(function(err, result){ var emails = []; for(var i =0; i< result.length; i++){ emails.push(result[i].split("@")[0]); } }) 但是,我不觉得这是有效的,因为在查询之后,我遍历结果并将它们存储在新数组中。 有没有更好的办法,只有alex , elizabeth , hannah查询? 这只是一个例子。 我想问你的build议,因为我有很多这样的情况。 (我正在寻找一种最有效的修剪方式,更新集合中的一些值)

在数组元素中添加一个子文档

下面的函数返回“结果”参数中的一个数组: module.exports.get = function (req, res) { objModel.find(function (err, results) { res.json({ record: results }) }) }; 我想将其引用集合logging列表添加到每个元素的一个新对象中。 就像下面这样: for (var i = 0; i < results.length; i++) { module.exports.get = function (req, res) { objModel.find({ _id: results[i]._id }, function (err, record) { results[i]["objNew"] = record }) }; } 我的完整代码如下所示: module.exports.get = function (req, res) […]

带有可能的空参数的find()语句

我试图弄清楚Mongoose和MongoDB是如何工作的…我对他们来说真的很陌生,我似乎无法想象如何根据find语句返回值,其中查询中的某些给定参数可能是空 – 是否有一个属性,我可以为此设置或什么? 为了进一步解释,我有一个网页,有不同的input字段,用于search公司,但他们不是所有的强制性。 var Company = mongoose.model('Company'); Company.find({companyName: req.query.companyName, position: req.query.position, areaOfExpertise: req.query.areaOfExpertise, zip: req.query.zip, country: req.query.country}, function(err, docs) { res.json(docs); }); 通过填写网页上的所有input字段,我得到一个结果,但只有那个匹配的特定的一个。 假设我只填写country ,它什么也没有返回,因为其余的都是空的,但是我希望返回所有在德国的行。 我希望我清楚地expression自己。

mongoose无效的运营商$匹配

我将根据email_address,firstname,lastname,type和phone_number来处理用户search。 电话号码search将精确search与国家代码,而其他人将包含&区分大小写的search。 所以,我写了下面的代码。 User.aggregate( [ { "$redact": { "$cond": [ { "$and": [ { "$match": { type: req.body.type, email_address: new RegExp((req.body.email_address || req.body.any || '').toLowerCase(), "i"), "firstname": new RegExp((req.body.firstname || req.body.any || '').toLowerCase(), "i") , "lastname": new RegExp((req.body.lastname || req.body.any || '').toLowerCase(), "i") } }, { "$or": [ { "$setIsSubset": [ [ { "$substr": [ […]

MongoDB – math计算

我需要计算这((86400 + (7200 – 28800)) % 86400)在MongoDB的总和。 可能吗? 或者需要在JavaScript上执行此操作。 mongo是否支持部门的剩余部分?

如何把variables放入我的查询? (mongoose)

需要为我的用户添加用户ID,这将是我的collections项数+1。例如,如果我有100个用户,下一个用户的用户ID将是101。 User.count().exec(function (e, count) { console.log(count); // Count = 15, correct. newUser.userId = count + 1; // Ain't working… }); 那么如何将“count”variables添加到我的查询中? 或者是更先进的方式,让自动增量(主键)在mongoose?

Mongodb nodejs查询语法错误

我想要这样的查询: 此查询将在friendrequest集合中select一个文档,其中sender_id等于“id1”且receiver_id等于“id2”或者sender_id等于=“id2”AND receiver_id等于“id1” 那么任何人都可以帮助我build立这个查询? select * from friendrequest其中(sender_id =“id1”和receiver_id =“id2”)OR(receiver_id =“id1”和sender_id =“id2”)mySql 错误: friendRequests.findOne({$or:[{$and:[{sender_id:req.body._id,receiver_id:req.body._uid2}],$and:[{sender_id:req.body._uid2,receiver_id:req.body._id}]}

带项目和filter的mongoose查询

我有查询: Model.aggregate([{ $lookup: { from: 'translations', localField: '_id', foreignField: 'item_id', as: 'translation' }, }, { $project: { "label": "$label", "items": "$items", "translation": { "$filter": { "input": "$translation", "as": "page", "cond": { "$eq": ["$$page.lang_key", lang] } } } } }]) 结果: [ { _id: 58b2ca5b9ac42bac7aaed48a, label: 'Main', items: [ [Object] ], translation: [ [Object] ] } […]

MongoDBcallback问题的嵌套调用

我正试图在我一直在工作的API中放置一个嵌套的MongoDB调用。 API的目标是根据练习执行更新人员目标。 我现在所面临的问题是试图将锻炼事件变成主要function,以使我能够计算“完整性”。 我欢迎您就此问题提出的任何意见。 大家好, 我试图把一个嵌套的MongoDB调用放在我一直在做的API中。API的目标是根据运行执行更新一个人的目标。我现在的问题是试图把运动事件放到主函数中使我能够计算“完整性”。 我欢迎您就此问题提出的任何意见。 module.exports.generateGoal = function(request, response) { //User ID var user_id = new ObjectId(request.params.id); //Goal Search Goals.find({ user_id: user_id, active_goal: true }, function(err, goals) { if (err) { //Error: send error to user throw err } else { if (goals.length == 0) { //No goals, do nothing return response.json({ success: […]

添加一个字段mongoose发现查询结果

我有这个员工架构在mongoose: employeesSchema = mongoose.Schema({ id: { type: Number, required: true }, firstName: String, lastName: String, title: String, managerId: { type:Number, required: false }, managerName: { type: String, required: false }, phone: String, mobilePhone: String, email: String, picture: String, }); 我有我的职能,find员工: module.exports.getEmployeeById = function(id, callback){ Employee.find({ {id: id} }, callback); } 但是我希望在我的Schema的所有字段旁边有一个结果字段,这个字段是具有managerId:id(该员工以下的员工)的员工的数组。 谢谢