Tag: mongodb

E11000在mongodbmongoose中重复键错误索引

以下是我在user.js模型中的user架构 – var userSchema = new mongoose.Schema({ local: { name: { type: String }, email : { type: String, require: true, unique: true }, password: { type: String, require:true }, }, facebook: { id : { type: String }, token : { type: String }, email : { type: String }, name : { type: […]

如何对mongoDB中的集合logging进行sorting

MongoDB的noob在这里… 好吧,我有一个学生的集合,每个人都有一个如下所示的logging….我想按照降序排列“types”:“作业”分数。 mongo shell上的这个咒语是什么样的? > db.students.find({'_id': 1}).pretty() { "_id" : 1, "name" : "Aurelia Menendez", "scores" : [ { "type" : "exam", "score" : 60.06045071030959 }, { "type" : "quiz", "score" : 52.79790691903873 }, { "type" : "homework", "score" : 71.76133439165544 }, { "type" : "homework", "score" : 34.85718117893772 } ] } 我正在尝试这个咒语…. doc = […]

在Mongoose中级联样式删除

有没有办法删除Mongoose中的父项的所有子项,类似于使用MySQL的外键? 例如,在MySQL中,我将分配一个外键,并将其设置为级联删除。 因此,如果我要删除一个客户端,所有的应用程序和关联的用户也将被删除。 从顶层: 删除客户端 删除抽奖 删除提交 抽奖和提交都有一个client_id字段。 提交中有一个sweepstakes_id和client_id的字段。 现在,我正在使用下面的代码,我觉得有一个更好的方法。 Client.findById(req.params.client_id, function(err, client) { if (err) return next(new restify.InternalError(err)); else if (!client) return next(new restify.ResourceNotFoundError('The resource you requested could not be found.')); // find and remove all associated sweepstakes Sweepstakes.find({client_id: client._id}).remove(); // find and remove all submissions Submission.find({client_id: client._id}).remove(); client.remove(); res.send({id: req.params.client_id}); });

MongoDB:如何用一个命令更新多个文档?

我惊讶地发现下面的示例代码只更新一个文档: > db.test.save({"_id":1, "foo":"bar"}); > db.test.save({"_id":2, "foo":"bar"}); > db.test.update({"foo":"bar"}, {"$set":{"test":"success!"}}); > db.test.find({"test":"success!"}).count(); 1 我知道我可以循环,并不断更新,直到他们都改变了,但这似乎非常低效。 有没有更好的办法?

在Meteor.js中使用多个Mongodb数据库

2 Meteor.Collections是否有可能从2个不同的mongdb数据库服务器中检索数据? Dogs = Meteor.Collection('dogs') // mongodb://192.168.1.123:27017/dogs Cats = Meteor.Collection('cats') // mongodb://192.168.1.124:27017/cats

MongoDB:聚合框架:获取每个分组ID的最新date文档

我想获得每个站的最后文件与其他所有领域: { "_id" : ObjectId("535f5d074f075c37fff4cc74"), "station" : "OR", "t" : 86, "dt" : ISODate("2014-04-29T08:02:57.165Z") } { "_id" : ObjectId("535f5d114f075c37fff4cc75"), "station" : "OR", "t" : 82, "dt" : ISODate("2014-04-29T08:02:57.165Z") } { "_id" : ObjectId("535f5d364f075c37fff4cc76"), "station" : "WA", "t" : 79, "dt" : ISODate("2014-04-29T08:02:57.165Z") } 我需要每站有最新的dt。 通过聚合框架: db.temperature.aggregate([{$sort:{"dt":1}},{$group:{"_id":"$station", result:{$last:"$dt"}, t:{$last:"$t"}}}]) 回报 { "result" : [ { "_id" […]

如何在MongoDB中select一个字段?

在我的MongoDB中,我有一个包含10个logging的学生集合,其中包含字段name和roll 。 这个collections的一个logging是: { "_id" : ObjectId("53d9feff55d6b4dd1171dd9e"),<br> "name" : "Swati", "roll" : "80", } 我想通过使用以下方法仅检索集合中所有10个logging的“滚动”字段: SELECT roll FROM student 我经历了许多博客,但都导致一个查询,它必须有WHERE子句。 例如: db.students.find({ "roll": { $gt: 70 }) 该查询等同于: SELECT * FROM student WHERE roll > 70 我的要求是只find一个领域没有任何条件。 那么,查询操作是什么呢。 提前致谢。

减lessMongoDB数据库文件大小

我有一个曾经很大(> 3GB)的MongoDB数据库。 从那时起,文件已被删除,我期望数据库文件的大小相应减less。 但是由于MongoDB保持分配的空间,文件仍然很大。 我在这里和那里读到,pipe理命令mongod –repair用于释放未使用的空间,但是我没有足够的空间来运行此命令。 你知道一个方法可以释放未使用的空间吗?

MongoDB,从数组中删除对象

文档: { _id: 5150a1199fac0e6910000002, name: 'some name, items: [{ id: 23, name: 'item name 23' },{ id: 24, name: 'item name 24' }] } 有没有办法从数组中拉出特定的对象? IE浏览器如何从items数组中拉出ID为23的整个项目对象。 我努力了: db.mycollection.update({'_id': ObjectId("5150a1199fac0e6910000002")}, {$pull: {id: 23}}); 不过,我很确定,我没有正确使用“拉”。 从我所了解的拉将从一个arrays,而不是一个对象拉场。 任何想法如何将整个对象拉出arrays。 作为一个奖金,我试图在mongoose/ nodejs这样做,以及不知道这种types的东西在mongooseAPI,但我找不到它。

Mongodb更新深度嵌套的子文档

我有一个深度嵌套的文档结构,如下所示: {id: 1, forecasts: [ { forecast_id: 123, name: "Forecast 1", levels: [ { level: "proven", configs: [ { config: "Custom 1", variables: [{ x: 1, y:2, z:3}] }, { config: "Custom 2", variables: [{ x: 10, y:20, z:30}] }, ] }, { level: "likely", configs: [ { config: "Custom 1", variables: [{ x: 1, […]