Tag: mongodb

如何从MongoDB 2.4中的数组内的文档中删除属性

在MongoDB 2.4中使用本地Nodejs驱动程序,并尝试从embedded在数组中的文档中移除属性。 例如,我怎样才能删除所有文件embedded在gym 562e7c1ae6c3e8ce29abfe05 grades数组? 所以从这个: { "_id" : ObjectId("562abaaffb5870b8fb9dca3f"), "grades" : [ { "_id" : ObjectId("562e7bbe70ee82bb290192bd"), "discipline" : "55aae5b7848b9c8bf4ecbb5c", "grade" : "55aae5b7848b9c8bf4ecbb4c", "gym" : "562e7c1ae6c3e8ce29abfe06", "registeredOn" : ISODate("2015-10-26T19:15:10.565Z") }, { "discipline" : "55aae5b7848b9c8bf4ecbb5c", "grade" : "55aae5b7848b9c8bf4ecbb4d", "gym" : "562e7c1ae6c3e8ce29abfe05", "registeredOn" : ISODate("2015-10-26T19:17:08.603Z"), "_id" : ObjectId("562e7c34e6c3e8ce29abfe06") }, { "discipline" : "55aae5b7848b9c8bf4ecbb5c", "grade" : "55aae5b7848b9c8bf4ecbb4d", "gym" […]

Node.js mongodb与callback的麻烦

所以我试图创build一个注册路线,检查用户是否先存在,我有一个单独的函数中的数据库调用,当它完成时需要返回true或false 。 问题是我不是很熟悉callback和整个asynchronous的东西,我已经search了一切似乎并没有工作不断给我。 TypeError: callback is not a function 这是我的代码任何帮助或方向将不胜感激。 function pullUserFromDatabase(username, callback) { console.log(username); //for debug mongodb.connect(url, function(err, db) { if(err) { console.log("didn't get far" + err) //for debug } var collection = db.collection(username); collection.findOne({username}, function(err, item) { if(err) { console.log("nope it broke" + err) //for debug } else { console.log("it worked" + JSON.stringify(item)) […]

MongoDB的随机logging

这是我第一次问StackOverflow的问题,我很抱歉,或多或less地重复或如果我发布了任何不正确的东西。 我试图按照从MongoDB的随机logging中的代码,但仍努力从我的节点MongoDB中检索4个随机logging。 在Mongo shell中,我可以用这个成功地检索4个随机logging: db.reccs.find( { genre: { $in: ['fantasy']} } ).limit(4).skip(Math.random() * db.reccs.count( { genre: { $in: req.query.test} } )) 但是,在我的Node.js server.js文件中,这段代码没有返回任何错误,并且find了前四个logging,而不是一个随机的4: app.get('/comics', function(req, res) { var comics = db.collection('reccs').find( { genre: { $in: req.query.test} } ).limit(4).skip(Math.random() * db.collection('reccs').count( { genre: { $in: req.query.test} } )).toArray(function(error, comics) { if (error) {console.dir(error+"error!")} res.send(comics) }) }) […]

如何在Meteor中为嵌套数组定义模式?

我正在使用SimpleSchema和meteor来构造数据库条目。 问题是,我有一个数组的数组和定义的模式不起作用。 这里是一个文档的例子: Courses.insert({ "persian_title":"persian title", "english_title":"english title", "free_for_all":true, "price":1000, "is_offer":false, "offer_price":500, "Seasons":[ { "title":"first Season", "free_for_all":false, "Episodes":[ { "title":"first Episode", "length":"12:10:00", "url":"test" }, { "title":"second Episode", "length":"0:10:00", "url":"test" }, { "title":"third Episode", "length":"14:10:00", "url":"test" } ] }, { "title":"Second Season", "free_for_all":false, "Episodes":[ { "title":"first Episode", "length":"12:10:00", "url":"test" }, { "title":"second Episode", "length":"0:10:00", "url":"test" }, […]

Heroku,当我在网上部署我的应用程序崩溃,但在我的本地机器上运行没有错误

Nov 04 09:50:57 eva-app-nodejs heroku/web.1: Starting process with command `npm start` Nov 04 09:50:59 eva-app-nodejs app/web.1: > Eva@0.0.1 start /app Nov 04 09:50:59 eva-app-nodejs app/web.1: > nodemon ./bin/www Nov 04 09:50:59 eva-app-nodejs app/web.1: [nodemon] 1.8.1 Nov 04 09:50:59 eva-app-nodejs app/web.1: [nodemon] to restart at any time, enter `rs` Nov 04 09:50:59 eva-app-nodejs app/web.1: [nodemon] watching: […]

用mongoose在不同的对象数组中find一个对象

我必须知道,如果一个项目是在这个模型的第一个或第二个数组中。 Model: _id: String, array1: [{ id: Number, name: String, timestamp: String }], array2: [{ id: Number, name: String, timestamp: String }], array3: [{ id: Number, name: String, timestamp: String }], array4: [{ id: Number, name: String, timestamp: String }] }); 我正在使用这些查询,但任何人都可以帮助我改进它,或者只有一个查询呢? Model.findOne({_id: userId , 'array1.id': item.id },function(error, res) { if(!res){ Model.findOne({_id: userId , 'array2.id': […]

Node.js $ http PUT请求不向服务器返回任何内容

我正在开发一个pipe理客户信息的应用程序。 我是新来的MEAN堆栈,不能为我的生活弄清楚我在这里做错了什么。 当我从视图中单击“更新客户”时,控制台打印更新的信息,但是当我尝试将该信息发送到服务器并更新我的表时,我什么也得不到。 客户实体与以前一样。 风景 <div class="col-sm-12" id="addButton" style="padding: 10px;" > <button class="btn btn-primary" ng-click="update()" ng-disabled="editCustomerForm.$invalid">Update Customer</button> 控制器 $scope.update = function(){ console.log($scope.existingCustomer); $http.put('/customers/' + $scope.existingCustomer._id, $scope.existingCustomer).success(function(response) { refresh(); $scope.existingCustomer={}; $scope.editOldCustomer = false; }) }; server.js文件 app.put('/customers/:id', function (req, res) { console.log(req.body); var id = req.params.id db.customers.findAndModify({ query: {_id: "mongojs.ObjectId(id)"}, update: {$set: {name: req.body.name, email: req.body.email, […]

在Nedb数据表中查找最小值?

我在Nedb中有如下的数据结构: ["UserId":"1446844665274","taskDone":"false","id":12,"_id":"1XURfx5uAZpgbh1i"] ["UserId":"1446844665274","taskDone":"false","id":10,"_id":"2I25xNAGTM7Iw2Af"] ["UserId":"1446844665274","taskDone":"false","id":3,"_id":"ESfi8ej5pbWBHV8o"] ["UserId":"1446844665274","taskDone":"false","id":9,"_id":"Etta7RLg2b09p9Bx"] 我正在尝试从表中筛选最小的id值的行。 如果有人可以build议我如何查询NEDB以获得最小的id值,这将是非常有帮助的。 我正在使用node.js和nedb。 谢谢。

无法使用mongoose保存

借助以下方面的帮助: 1. https://stackoverflow.com/a/30164855/2168120 2. mongodb自动增量 app.js: //some codes var mongoose = require('mongoose'); var connection = mongoose.createConnection("mongodb://localhost/dfp", function(err){ if(err){ console.log(err); }else{ console.log("connected to mongodb from app"); } }); 模式: var userSchema = mongoose.Schema({email: String, fname: String, lname: String, password: String, gender: String, mobile: String, dob:{ type: Date, default: Date.now }, ip: String, doj:{ type: Date, default: […]

数组更新在mongoDB

我有这样的mongoose模式: var mySchema = new mongoose.Schema({ user: String, photos: [{ url: String, thumbnail: String, time: Date, mainPhoto: Boolean }] }); 现在,我想制作一个“setMainPhoto”函数 – 对于给定的id – 将mainPhoto标志设置为true,并将其设置为false为其他照片。 有这样一个文件: { "_id": { "$oid": "56269dea07a455920a21bca7" }, "user": "someUser", "photos": [ { "time": { "$date": "2015-05-25T08:37:56.000Z" }, "thumbnail": "https://s3-eu-west-1.amazonaws.com/…jpg", "url": "https://s3-eu-west-1.amazonaws.com/…jpg", "_id": { "$oid": "56269e2e07a455920a21bcab" }, "mainPhoto": false }, { […]