Tag: mongodb

不会将数据插入到具有nodeJS的mongoDB的“用户”集合中

我遇到麻烦,用nodeJS向mongoDB插入数据。 这是我的server.js文件的一部分: app.post('/users', function(req, res, next){ db.collection('users', function(err, usersCollection){ usersCollection.insert(req.body, {w : 1} , function(err){ return res.send(); }); }); }); 这是我的控制器在我的index.html文件中: app.controller('SLController', function ($scope, $http){ $scope.submitSignup = function(){ var newUser = { username: $scope.username, email: $scope.email, password: $scope.password }; $http.post('/users', newUser).then(function(){ alert('Success'); }); } /*$scope.signin = function (){ $http.put('/users/signin', {username: $scope.username, password: $scope.password}) .then(function(){ alert("Successfully […]

Ember:对store.query的响应预计是一个数组,但它是一个logging错误

我不断收到错误: 错误:断言失败:对store.query的响应预期是一个数组,但是它是单个logging。 请将您的响应封装在数组中,或使用store.queryRecord查询单个logging。 这是我的后端: router.route('/') .post(parseUrlencoded, parseJSON, function (request, response) { var advancedStanding = new models.AdvancedStandings(request.body.advancedStanding); advancedStanding.save(function (error) { if (error) response.send(error); response.json({advancedStanding: advancedStanding}); }); }) .get(parseUrlencoded, parseJSON, function (request, response) { //var l = parseInt(request.query.limit); //var o = parseInt(request.query.offset); var AdvancedStanding = request.query.filter; if (!AdvancedStanding) { models.AdvancedStandings.find(function (error, advancedStandings) { if (error) response.send(error); response.json({advancedStanding: […]

在平均申请中的租赁

我正在尝试在MEAN中创build一个多租户应用程序,根据某些variables的值,它应该select一个mongoDB连接forms的连接池,并对集合执行CURD操作。 var mongoose = require('mongoose'); var connectionPool = {}; connectionPool['dbName1'] = mongoose.createConnection('mongodb://localhost/database1'); connectionPool['dbName2'] = mongoose.createConnection('mongodb://localhost/database2'); connectionPool['dbName3'] = mongoose.createConnection('mongodb://localhost/database3'); 连接对象在使用CURD操作时无法正常工作。 错误 – db.collection(…)。find(…).exe不是函数 谢谢。

如何使用node-mongodb-native驱动程序删除文档?

操作返回说deleted: 0 const res = await ctx.db.collection(this.col).removeOne({ _id: ctx.params.id }); 不知道我在这里做错了什么。 { _id: <id> } GET请求似乎正常工作。 ctx.params.id被定义,并且与数据库中的ObjectId相同。 根据这个文档,你可以做collection.removeOne() (参见示例2) https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#remove // Remove all the document collection.removeOne({a:1}, {w:1}, function(err, r) { test.equal(null, err); test.equal(1, r.result.n); db.close(); });

Mongoose更新条件匹配错误的对象

模型模式 const PollSchema = new Schema({ title: { type: String, trim: true, required: true }, choices: [ { title: { type: String, trim: true, required: true }, votes: { type: Number, default: 0, min: 0 }, } ], date: { type: Date, default: Date.now }, url: { type: String, required: true, unique: true }, }) […]

Mongoose不返回正则expression式子string

Reference.find( {$and: [ {'url': url}, {"text" : {$regex : text, '$options' : 'i'}} ]}) 每当我用“Hello World”testingtext时,我都会返回那些包含文本的文档。 但是,如果我要做“Hello Worl”,即使使用相同的url ,我也不会收到任何回应。 我已经尝试了很多ReGex的实现,到目前为止没有任何工作。 如果有问题,我正在使用Mongo 3.2.11。

Mongoose:查询未完成时如何查找.ById

我正在构build一个search查询,即使在用户input的时候,它也会通过它的IDfind一个数据库对象。 ordersRouter.route('/searchorder/:term') .get(function(req, res){ term = req.params.term; console.log(term); Orders.findById(term) .populate({ path: 'userPurchased products.product', select: '-username -password' }) .exec(function(err, orders){ if (err) throw err; res.json([orders]); }); }); 这里的问题是,当这个term和ID不完全一样时,它什么都不会返回。 我怎样才能返回部分term ID? 编辑:我的订单模式架构 var orderSchema = new Schema({ orderId: { type: String }, userPurchased: { type: Schema.Types.ObjectId, ref: 'users' }, products: [ { product: { type: Schema.Types.ObjectId, ref: […]

MongoDB / Mongoose – 通过执行Addition更新文档中的数字

我有一个mongoDB集合,可以存储时间和成本。 当用户第一次input数据时 ,文件有点像这样: { "fullName" : "john smith", "company" : "xyz", "email" : "john.smith@xyz.com", "timeSaved" : 50 //this is in minutes "moneySaved" : 10 //this is in dollars } 现在,当用户再次input数据时,我发现这个用户是否已经存在一个文档,如果有的话,我可以得到它。 这次用户input了: 节省时间55 钱已保存10 我想将新的值添加到现有的值。 所以文件显示这个之后: "timeSaved" : 105 // old 50 + new 55 "moneySaved" : 20 // old 10 + new 10 什么是实现这个最好的方法。 在MongoDB […]

我在哪里可以将这行代码放在Meteor的新文件结构中?

我从Meteor.com网站转到了这里。 我是这个Javascript库的新手,虽然我有一个如何工作的概念框架,但我想用它来获得一些真实世界的经验。 不幸的是,Meteor.com提供的教程和当前下载的实际代码是完全不同的。 它看起来像最初,所有的js在一个文件,现在的服务器function,其余的js被分成两个单独的文件,我一直无法find文件,将指导我两个新js中的哪一个我要添加这段代码来创build我的第一个集合: Tasks = new.Mongo.Collection('tasks') 客户端的main.js文件如下所示: import { Template } from 'meteor/templating'; import { ReactiveVar } from 'meteor/reactive-var'; import './main.html'; Template.hello.onCreated(function helloOnCreated() { // counter starts at 0 this.counter = new ReactiveVar(0); }); Template.hello.helpers({ counter() { return Template.instance().counter.get(); }, }); // templates can have helpers which are just functions and events and this […]

MongoDB Node.js模块“mongo”不被识别为内部或外部命令

我正在学习Youtube的Code Academy的Node.js教程,目前的video编号为19.在3:10他可以看到他input了mongo,然后切换到数据库。 但在我的情况下,它提供以下错误'mongo' is not recognized as an internal or external command, operable program or batch file. 为什么发生这种情况? 我完全按照video中所示的步骤进行操作。 在我的情况下,将其连接到数据库并插入2个文档(如图所示)。 但在下一步它给这个错误。 请帮我开发。