Tag: mongoose

如何*不*保存mongoose文件?

我有一个mongoose字段X.我查询并更新其数据。 在某些情况下,它必须被复制,并且只有新的副本必须用新数据保存到数据库(原始数据必须与查询前保持一致)。 但不知何故,mongoose保存原始文件,即使我没有问一个.save()命令。 X.findById(req.params.id) .exec(function (err, original) { original.name = 'newName'; if(condition){ var copy = new X(original); copy.isNew = true; copy._id = mongoose.Types.ObjectId() X.collection.insert(copy, callback(){}) }else{ original.save(); } 有任何想法吗?

MongoDB:从另一台服务器复制数据后,不查询

我有一个生产服务器,我们有一个nodejs应用程序和一个MongoDB数据库。 我们在服务器上有一个虚拟机,应用程序和数据库位于那里。 现在,我们要在另一台服务器上构build一个开发环境。 因此,我们已经将虚拟机复制到另一台服务器。 但现在,当我们的应用程序查询MongoDB时,它不会得到任何数据。 但是,它连接到数据库,MongoDB的日志和日志forms的应用程序显示给我们。 而且,当我们在整个mongo shell中查询数据库的时候,我们得到的数据。 应用程序使用mongoose连接到数据库。 因此,我们试图复制生产服务器的数据为: 在生产服务器上执行mongoexport,然后在其他机器上执行mongoimport,但是什么也没有。 在生产服务器上执行一个mongodump,然后在其他机器上执行一个mongorestore,但是什么都没有。 服务器的特点如下: 两个服务器的操作系统:Ubuntu 16.04 MongoDB版本:3.2.11 然后,因为我无法获得数据,我试图将其复制到我的本地主机。 但是这不是问题,这只是附加信息: 我的电脑操作系统:Ubuntu 14.04(32位) MongoDB版本:3.2.4

Mongoose模型访问模式属性

我有以下架构 import * as mongoose from "mongoose"; let _posteSchema = new mongoose.Schema({ … updatedAt: {type: Date, default: Date.now}, … }); export default (_posteSchema); 而在另一个文件我正在尝试这个 import * as mongoose from "mongoose"; import posteSchema from "./poste-schema"; let Poste = mongoose.model("Poste", posteSchema); let _poste = new Poste(); _poste.updatedAt = Date.now(); 我得到了错误“TS2339:Property'updatedAt'在types'Document'上不存在。它曾经工作,而且我正在为此哭泣… 任何帮助将是如此伟大;) (使用“mongoose”:“^ 4.11.12”)

mongoose得到一个除了一个随机元素

我有一个MongoDB的文章集合。 我select了一篇我想呈现的文章,并且我想要随机select其他两篇文章。 我想在我的collections中挑选两篇不一样的文章,而不是我之前select的文章。 一直在这个问题上几个小时,寻找一个解决scheme,但只发现如何随机挑选一个元素,但不是除了一个… 这是我现在拥有的: article.find({}, function(err, articles{ var articleChosen = articles.filter(selectArticleUrl, articleUrl)[0]; article.find({}) .lean() .distinct("_id") .exec(function(err, arrayIds){ var articleChosenIndex = arrayIds.indexOf(articleChosen._id); arrayIds.splice(articleChosenIndex, 1); chooseRdmArticle(arrayIds, function(articleRdm1Id){ var articleRmd1 = articles.filter(selectArticleId, articleRdm1Id)[0]; var articleRdm1Index = arrayIds.indexOf(articleRdm1Id); arrayIds.splice(articleRdm1Index, 1); chooseRdmArticle(arrayIds, function(articleRdm2Id){ var articleRmd2 = articles.filter(selectArticleId, articleRdm2Id)[0]; // do stuff with articleChosen, articleRmd1 and articleRmd2 }) }) }) […]

mongoose计数返回错误的数据

Meeting.count(countQuery).then(countBookings) .then(findUser) . // a few more then blocks, that are not relevant to my problem . . .catch((err) => { console.log('The meeting error is ', err); }) 这是我的计数查询对象: let countQuery = { startDate: {$gte: date1, $lte: date2}, teamId: req.body.teamId }; 这是我的预约function: const countBookings = (data) => { console.log('reached countBookings') if (data >= 6 || […]

mongoose:删除操作 – 看到它的输出

我正在执行从名为userModel集合中remove操作。 我需要查看已删除文档的ID,以便在其他集合中采取更多操作。 这就是我正在做的事情: userModel.remove(myQuery, function (err, result) {…}) 在result我只看到被删除的文件数量。 我怎样才能看到他们的ID?

没有findMongoDB游标

我正在用节点js使用mongodb(mongoose模块) 我正在使用限制和跳过function处理大约100,000个文档(每个1000个)。 我的处理很好,但经过一段时间,它给了我一个错误。 { MongoError: Cursor not found, cursor id: 62783806111 at Function.MongoError.create (/home/admin/Pictures/duplicayProj1/node_modules/mongoose/node_modules/mongodb-core/lib/error.js:31:11) at /home/admin/Pictures/duplicayProj1/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:483:72 at authenticateStragglers (/home/admin/Pictures/duplicayProj1/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:429:16) at Connection.messageHandler (/home/admin/Pictures/duplicayProj1/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:463:5) at Socket.<anonymous> (/home/admin/Pictures/duplicayProj1/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:339:20) at emitOne (events.js:115:13) at Socket.emit (events.js:210:7) at addChunk (_stream_readable.js:252:12) at readableAddChunk (_stream_readable.js:239:11) at Socket.Readable.push (_stream_readable.js:197:10) at TCP.onread (net.js:589:20) name: 'MongoError', message: 'Cursor not found, cursor id: 62783806111', ok: 0, errmsg: […]

使用feathersjs创build后无法从对象中删除_id字段

我想在插入数据后在我的节点应用程序中修改我的hook.data对象。 其实我不能。 create: [function(hook, next) { delete hook.data._id; hook.data = { problem: hook.data } postJson(hook.app.get('jsprintUrl'), hook.data) .then(data =>{ hook.result = data; next() }) }] 结果:仍然存在_id { "_id": "59ca334e7bc4e06b140aadf9", "algorithm": [ { "name": "SA" } ] }

最好的方法来包装mongoosevalidation错误

我们知道mongoose为我们提供了一个简单的方法来进行validation。 但是,假设你使用express + mongoose来构build一个微服务; 和一些客户端(可能是networking应用程序,手机应用程序等)需要消耗它。 通常,我更喜欢用简单的错误代码和消息来回应JSON。 在大多数情况下,客户可以根据他们向用户显示哪种语言来创build自己的消息。 默认情况下,如果我们从mongoose捕获错误,我们可以得到JSON响应,如: JSON响应 { "errors": { "price": { "message": "Path `price` (-1) is less than minimum allowed value (0).", "name": "ValidatorError", "properties": { "min": 0, "type": "min", "message": "Path `{PATH}` ({VALUE}) is less than minimum allowed value (0).", "path": "price", "value": -1 }, "kind": "min", "path": "price", "value": -1, […]

如何使用mongoose和asynchronous瀑布模型在MongoDb中存储数据

你好我是新的节点,我想保存数据在mongoose。 问题是有3个collections单位,build筑和部分。 build筑图是: var buildingsSchema=new Schema({ buildingname:String, status:String }); 部分模式是: var sectionsSchema=new Schema({ section_type:String, buildings:{ type: Schema.Types.ObjectId, ref: 'buildings' }, status:String }); 单位的架构是: var unitsSchema=new Schema({ unit_type:String, unit_num:String, unit_ac_num:Number, buildings:{ type: Schema.Types.ObjectId, ref: 'buildings' }, sections:{ type: Schema.Types.ObjectId, ref: 'sections' }, shares:Number }); 在部分有一个build筑物的身份证和在单位有build筑物和部分的身份证 现在我已经使用xlsx插件将上传的Excel文件转换为json: var wb = XLSX.readFile("uploads/xls/" + req.file.filename); data = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], {header:1}); […]