Simple MongoDB query find item age > 10 learnyoumongo find function - javascript

I'm going through learnyoumongo and I'm stuck on part 3. Basically a test database is included in the challenge, it is full of parrots, and the goal is to select the parrots whose age is greater than the input. I'm getting a weird error and google is full of mongo 2.x solutions to not exactly the same problem and I'm using mongo 3.0
This is the javascript code:
var mongo = require('mongodb').MongoClient;
var parsedInput = parseInt(process.argv[2]);
var results;
mongo.connect('mongodb://localhost:27017/learnyoumongo', function(err, db){
results = db.collection('parrots').find({ age: { $gt: parsedInput } } ).toArray(function(err, doc) //find if a value exists
{
if(doc) //if it does
{
console.log(doc);
}
else{
console.log(err);
}
});
//console.log(results);
db.close();
});
This is the weird error message:
PS C:\git\learnyoumongo> node .\test.js { [MongoError: server localhost:27017 sockets closed]
name: 'MongoError',
message: 'server localhost:27017 sockets closed' }
I tried restarting mongo, but I'm still not able to pull any of the 'parrots' data out. Even with just find({})

The problem was two pronged - The main issue was I was expecting to be able to run the query with node test.js, and see the results from the parrots collection. But learnyoumongo has atomic tests, meaning they clear the database entirely before and after, so the only way to test was learnyoumongo test.js, and I kept getting an empty result set running the node command.
The other issue was with db.close(), you can't just call db.open and then db.close, because open is async and it would close right after opening, hence the sockets closed error. So you put db.close in the toArray function, or in any other callback of db.open

Related

MongoError: cursor killed or timed out - Meteor timeout settings ineffective

My Meteor 1.2.1 program threw MongoError: cursor killed or timed out in a find().forEach() loop, so i found this page that says this code prevents that:
var myCursor = db.users.find().noCursorTimeout()
However, the driver docs and my Meteor say that method doesn't exist: Object [object Object] has no method 'noCursorTimeout'
Mongo autoReconnect is enabled by default and didn't help, nor did the Meteor forum, or even .find({}, {timeout:false}) according to this comment.
2016-07-20 11:21:37 Update started
2016-07-20 11:37:21 Exception while invoking method 'updateCollections' MongoError: cursor killed or timed out
Maybe Meteor got confused by the failed SOAP call at 2016-07-20 09:34:57?
"error": {
"errno": "ETIMEDOUT",
"syscall": "connect",
"code": "ETIMEDOUT"
},
Assuming maxTimeMS would help in this case you can access it by working with rawCollection object instead of the Meteor collection itself.
It's quite simple:
var rawCollection = Meteor.users.rawCollection();
var cursor = rawCollection.find({}).maxTimeMS(5000);
var myData = fetchCursor(cursor);
Where fetchCursor is a simple fiber-aware helper function that can be implemented like this:
var fetchCursor = Meteor.wrapAsync(function fetchCursor (cursor, cb) {
cursor.toArray(cb);
});
Though, I am not sure if this method is exactly what you're looking for.
Edit
If you don't need the entire array of documents but you want to process each one of them independently it may be better to use each instead of toArray, e.g.
var fetchCursor = Meteor.wrapAsync(function fetchCursor (cursor, cb) {
cursor.each(function (err, doc) {
if (err) return cb(err);
if (!doc) return cb(null, { done: true }); // no more documents
// do something with the document ...
});
});

Firebase's .once() Query Method Doesn't Stop Listening After Being Called [duplicate]

This question already has answers here:
node process doesn't exit after firebase once
(4 answers)
Closed 7 years ago.
I'm a totally new Firebase user, trying to build a simple application -- and I'm running into an odd issue that I haven't been able to find a solution to.
Here's what I'm doing.
I've got a simple Firebase database of users, that looks something like this:
{
"users": {
"randomlyAssignedId": {
"phoneNumber": "+18882223333"
}
}
}
The idea is that I'll have one user object (and each user object only contains a phoneNumber field) for each user using my service.
I've got this stored in Firebase and working already.
Now, here's where things are getting tricky. I want to write a query which returns a user by their phoneNumber. So, after reading the Firebase docs, here's what I came up with:
var Firebase = require('firebase');
var users = new Firebase('https://demo.firebaseio.com/users');
users.orderByChild('phoneNumber').equalTo('+18882223333').limitToFirst(1).once('value', function(snapshot) {
console.log('API call succeeded:', snapshot.val());
}, function(err) {
console.log('Firebase returned an error:' err);
});
When I run this code sample, the user object is logged to the console successfully (it found the match, yey!), however -- the callback never finishes.
According to the Firebase docs for .once(): https://www.firebase.com/docs/web/api/query/once.html the success (or error callback) should fire exactly ONE time.
This doesn't appear to be happening :(
Could anyone tell me if this is the desired behavior, or if I'm doing something wrong?
Thanks! <3
I think what you're expecting is just a little off.
You have 2 functions in your .once() call :
.once("value",function(snapshot){
....
},function(err){
console.log('Did not find a user in the database:');
console.log(err);
})
However, the 2nd function where you are looking for a possible err value is not going to be utilized if the data is not found. The 2nd function is there to catch a lack of authorization/permission. I think you are looking for something like this:
.once("value",function(snapshot){
if(snapshot.val()){
console.log('Found existing user by phoneNumber in database:');
console.log(snapshot.val());
}else{
console.log('Did not find a user in the database:');
console.log(err);
}
},function(err){
console.log('Not authorized');
console.log(err)
})
If your data (user's phone number) is not found in the database then the result of the snapshot.val() will be null. The 2nd callback function will only contain an err value if you have auth rules set on your firebase and they are not met when making this request.
You can read more here:
https://www.firebase.com/docs/web/api/query/once.html

Node.js and mongodb access mongodb

I'm trying to set up mongodb on Windows 8 using node.js, Does anyone know why im getting this error. C:\users\phill\node_modules\mongodb\lib\mongodb\mongo_client.js:359 it also says at collection = db collection,,, can't call method 'collection' of null. I'm having a hard time setting it up. My goal is to be able to add to mongo db, and see that I add or pull up what I added, but adding something is good enough for me for now. I'm trying every thing I can find, even straight from the website, I tried everything I see on here as well. Think it maybe it's the way I have things set up. My node.js is saved in my c: drive there is a file that says, program files(86x) in there I have node_modules, npm and such. The path ends up being, computer > windows (C:) > program files(86x) > nodejs. My Mongodb is saved right on my C: drive the path end up being windows (C:) > mongodb-win32-x86_64-2008plus-2.4.8. In my C: I also created a file data and in it created another db. I have been told i should just use mongoose, I'm just learning so i open to any advice, links or anything that will help. I have one last question as well, i learned php and then found out about sql injections and stuff like that, i am not seeing anything about security at all, should i expect the same as well. For this i get text not defined, but i have been getting errors with everthing i have done, best i did was get stuck on a right concern screen.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, db) {
test.equal(null, err);
test.ok(db != null);
db.collection("replicaset_mongo_client_collection").update({a:1},
{b:1}, {upsert:true}, function(err, result) {
test.equal(null, err);
test.equal(1, result);
db.close();
test.done();
});
});
Tried this as well and getting a error,C:\users\phill\node_modules\mongodb\lib\mongodb\mongo_client.js:359.... at collection = db collection,,, can't call method 'collection' of null. im calling it in command prompt node filename.js I'm saving it where my node.js file is, I have pulled up files before and created a server.
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');
var db = new Db('test', new Server('localhost', 27017));
// Fetch a collection to insert document into
db.open(function(err, db) {
var collection = db.collection("simple_document_insert_collection_no_safe");
// Insert a single document
collection.insert({hello:'world_no_safe'});
// Wait for a second before finishing up, to ensure we have written the item to disk
setTimeout(function() {
// Fetch the document
collection.findOne({hello:'world_no_safe'}, function(err, item) {
assert.equal(null, err);
assert.equal('world_no_safe', item.hello);
db.close();
})
}, 100);
});
In your first code example, you said:
For this i get text not defined
I assume you meant "test not defined?" Your script only requires the mongodb library, and I don't believe test is a core nodejs function, so that would explain the error.
To reference the driver documentation for db.collection(), an assert library is used, but also properly imported (as you did in your second example).
Within your callback to db.open(), you don't check if an error occurred. That might shed some light on why db is null in that function.
Regarding your question about the equivalent of SQL injection with MongoDB, the main areas of concern are places where you might pass untrusted input into evaluated JavaScript, or using such input to construct free-form query objects (not simply using a string, but more like dropping an object into your BSON query). Both of these links should provide more information on the subject:
What type of attacks can be used vs MongoDB?
How does MongoDB address SQL or Query injection?

Why do the Node.js mongodb driver methods not invoke callbacks

In the example below, I expect to see two lines from the console.log statements in the terminal but I only get one since the second callback is never fired. I'm using mongodb (2.4.8_1) and Node.js (0.10.22) from MacPorts with the official Node.js mongodb driver (1.3.20) from NPM (1.3.15). How can this happen?
var mongodb = require('mongodb');
mongodb.MongoClient.connect('mongodb://localhost:27017/test', function (err, db) {
var query = { _id: new mongodb.ObjectID };
console.log('connect!');
db.collection('test').findOne(query, function (err, doc) {
console.log('findOne!');
});
});
Every time you run new mongodb.ObjectID it creates a new ID value - which you then instantly search for without inserting a document with. So how can it find a document, and hence show the second log line?

Entering data with MongoDb and Node.js

So I'm trying to enter data into a mongodb collection with node. As far as I can tell I have access to the collection.
var collection = db.collection("whatsGoingOnEvents");
if(collection){
console.log("hitting stream");
var stream = collection.find({time: parsedObject.time, endTime: parsedObject.endTime, lon:parsedObject.lon,lat:parsedObject.lat}).stream();
console.log(stream);
stream.on("data",function(data){
console.log("data");
console.log(data);
if(!data){
collection.insert(parsedObject);
console.log("hitting insert");
}
});
stream.on("end",function(){
//dosomething
});
}
parsedObject may or may not have all of those fields - should it matter? I thought if the field was not there then collection.find() is just looking for time to be "undefined", which is still technically a value.
I never hit console.log("data") so I never insert documents. I've been trying to follow this link.
and so am not sure why the insert is not happening. I know that nothing is being added from db.collection.stats();, which tells me the size of the collection is 0.
Oh also, this is what I'm using to connect to Mongo-
var mongo = require('mongodb').MongoClient;
EDIT--
I tried the answer below - that resulted in this error-
lib/mongodb/connection/server.js:481
throw err;
^
Error: Cannot use a writeConcern without a provided callback
at insertAll (/Users/psanker/Google Drive/Coding/Javascript/WhatsGoingOn/node_modules/mongodb/lib/mongodb/collection.js:332:11)
at Collection.insert (/Users/psanker/Google Drive/Coding/Javascript/WhatsGoingOn/node_modules/mongodb/lib/mongodb/collection.js:91:3)
^The above occurred because I hadn't added a callback to the insert.
If your query doesn't match any records (which would seem logical, given that you write that the collection size is 0), the data event handler will never get called (because that will only be called when there's an actual result).
I think you're better off with using findOne and a regular callback:
collection.findOne({ params }, function(err, result) {
if (err)
throw err;
if (result === null) {
collection.insert(parsedObject, { w: 0 });
}
});
Or even an upsert.

Categories

Resources