mongojs

MongoDB and MongoJS - can't get runCommand to work for text queries

无人久伴 提交于 2019-12-06 00:47:12
My goal is to use MongoDB's (2.4.4) text command from Node. It works fine from the command line. Based on this previous SO issue: Equivalent to mongo shell db.collection.runCommand() in Node.js , I tried using MongoJS (0.7.17) but can't make it go. Here is the code: mongojs = require('mongojs'); var products = mongojs('localhost:27017/mydb').collection('products'); products.runCommand('text', {search: 'a'}, function (err, docs) { ... }); docs returns undefined and err is null. I can execute a normal function such as products.find() fine... and I can execute the search on the MongoDB command

TypeError: Cannot read property '_id' of undefined

*爱你&永不变心* 提交于 2019-12-05 16:14:43
问题 I am getting error "TypeError: Cannot read property '_id' of undefined" on a simple post request to save a document to the collection called books , My payload looks like this: { "name": "practical view", "author": "DN", "location": "room 50" } And I am just doing db.books.save() in my route in express. Since I am not passing the id, this should ideally work, but not in this case. Below is the full error dump I am getting on my node server: C:\NodeProjects\ExpressTutorial\Library\node_modules

Mongo failed to connect to server on first connect

南笙酒味 提交于 2019-12-04 20:13:28
I know that's a way discussed issue, and have gone through almost every topic I found on SO and google in general about it, but still no resolution! The problem At random points in time, the following error occurs MongoError: failed to connect to server [127.0.0.1:20000] on first connect at Pool.<anonymous> (/home/user/node_modules/mongodb-core/lib/topologies/server.js:326:35) at emitOne (events.js:96:13) at Pool.emit (events.js:189:7) at Connection.<anonymous> (/home/user/node_modules/mongodb-core/lib/connection/pool.js:270:12) at Object.onceWrapper (events.js:291:19) at emitTwo (events.js

MongoError: socket hang up

≯℡__Kan透↙ 提交于 2019-12-04 17:51:22
I am trying to connect to the mongodb database on mongolabs(mlabs). I connect successfully when I run the code on my local computer and server.But When I run on my aws server I get this error database error { [MongoError: socket hang up] name: 'MongoError', message: 'socket hang up' } Code trial.js: var express = require('express'); var app = express(); var mongoose = require('mongoose'); var mongojs = require('mongojs'); var db = mongojs('mongodb://user:pass@ds01312192.mlab.com:133492/database', ['mohd'], { ssl : true }); db.on('error', function (err) { console.log('database error', err); });

How to dynamically $set a subdocument field in mongodb? [duplicate]

牧云@^-^@ 提交于 2019-12-03 13:10:16
This question already has an answer here: Nodejs Mongo insert into subdocument - dynamic fieldname 2 answers I've run into a situation where I need to dynamically update the value of a field in a subdocument. The field may or may not already exist. If it doesn't exist, I'd like mongo to create it. Here's an example document that would be found in my Teams collection, which is used to store members of any given team: { _id : ObjectId('JKS78678923SDFD678'), name : "Bob Lawblaw", status : "admin", options : { one : "One", two : "Two" } } And here's the query I'm using (I'm using mongojs as my

Inserting the current datetime in mongodb

时光总嘲笑我的痴心妄想 提交于 2019-12-03 08:46:37
问题 I have been having trouble inserting an actual datetime object in mongodb using the mongojs driver for nodejs. Any help? var currentdate = new Date(); var datetime = currentdate.getDate() + "/" + (currentdate.getMonth()+1) + "/" + currentdate.getFullYear() + " @ " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds(); db.test.update({ conversation: conv },{ $push:{ messages: { message: message, pseudo: name, current_date: datetime }} },{upsert: true}); 回答1

how to insert a .json document to mongo server by mongojs in node

萝らか妹 提交于 2019-12-01 18:03:06
I'm new with nodeJS and MongoDB. I have this code: var fs = require('fs'); var mongojs = require('mongojs'); var db = mongojs('monitor', ["configurations"]); fs.readFile('json/object1.json', 'utf8', function (err, data) { if (err) throw err; console.log(data); db.configurations.insert(data, function(err, doc) { console.log(data); if(err) throw err; }); }); no data insered to the mongodb, and I've got no error. the both console.log(data) print the json string as well. Try to parse it as JSON before inserting it into the document fs.readFile('json/object1.json', 'utf8', function (err, data) { if

MongoDB query for document older than 30 seconds

走远了吗. 提交于 2019-11-30 21:37:10
Does anyone have a good approach for a query against a collection for documents that are older than 30 seconds. I'm creating a cleanup worker that marks items as failed after they have been in a specific state for more than 30 seconds. Not that it matters, but I'm using mongojs for this one. Every document has a created time associated with it. We are assuming you have a created_at or similar field in your document that has the time it was inserted or otherwise modified depending on which is important to you. Rather than iterate over the results you might want to look at the multi option in

node.js mongojs findOne callback returning error as null

a 夏天 提交于 2019-11-29 10:56:38
Presently being driven up the wall by this error. I am running a node.js app with the mongojs wrapper for mongodb. I Started mongod on the default port, then ran var db = require('mongojs').connect('localhost:27017/my_db'); var users = db.collection('users'); users.findOne({'fb_id' : fbUserMetadata.id}, function(err, user) { console.log(err); console.log(user); debugger; }); however err and user are both 'null'. To my knowledge, err should be populated with some kind of data even if it doesn't find anything. How do I get the callback function to work properly? Pardon the newbie question. When

node.js mongojs findOne callback returning error as null

烂漫一生 提交于 2019-11-28 04:25:08
问题 Presently being driven up the wall by this error. I am running a node.js app with the mongojs wrapper for mongodb. I Started mongod on the default port, then ran var db = require('mongojs').connect('localhost:27017/my_db'); var users = db.collection('users'); users.findOne({'fb_id' : fbUserMetadata.id}, function(err, user) { console.log(err); console.log(user); debugger; }); however err and user are both 'null'. To my knowledge, err should be populated with some kind of data even if it doesn