0

Extracting an integer from the return of a MongoDB query, in Javascript.

I'm planning to create some server-side processed queries for my MongoDB setup, using stored procedures in javascript. As Proof-of-concept, I'm trying to walk through the steps of extracting values from my existing test database in the Mongo shell, making them accessible for processing in javascript.

My database consists of entries which includes a domain name, and the number of hits it has seen:

{"dom":"domain1.org", "hits": 38}
{"dom":"domain2.com", "hits": 12}

For Proof of concept, I want to write a function 'addDomainHits' which will do the following:

db.eval('addDomainHits("domain1.org","domain2.com")');
50

[Note 1: I'm perfectly aware that I could use MongoDB aggregations to perform this particular function, but that isn't the answer; this is just a Proof of concept; I actually want to do a lot more inside the functions I actually write.]

[Note 2: Yes, this is horribly insecure, and open to code injection. I'll deal with that later. Again, this is just Proof of Concept.]

So, I tried to get the integer value into a variable in the MongoDB Shell, and that's where I run into problems.

$ mongo
MongoDB shell version: 2.6.11
connecting to: test

> use test_database
switched to db test_database

> var r=db.test_collection.find({"dom":"domain1.org"},{hits:true,_id:false})
> r
{ "hits" : 38 }

Now, I want to get the '38' into simple integer variable, so I can do processing similar to:

> a=2
2
> b=3
3
> a+b
5

but, no joy:

> r=db.test_collection.find({"dom":"domain1.org"},{hits:true,_id:false})
> r
{ "hits" : 38 }

> var r=db.test_collection.find({"dom":"domain1.org"},{hits:true,_id:false})
> r.hits
>

Notice that no value got returned

slight variant:

> var r=db.test_collection.find({"dom":"domain1.org"},{hits:true,_id:false})
> var h=r.hits
> h
>

OR

> var r=db.test_collection.find({dom:"seagoedd.org"},{hits:true,_id:false})
> var h=r['hits']
> h
>

So, in this example, how can I get a simple '38' into a variable?

3
  • try adding .toObject() on the end like this var r=db.test_collection.find({dom:"seagoedd.org"},{hits:true,_id:false}).toObject() Commented Mar 7, 2016 at 22:58
  • Running JavaScript code on the server ( while supported ) is never really a great idea. This is not "stored procedures" nor is the concept supported by MongoDB in any way. Your operations are generally better placed using the aggregation framework or mapReduce as appropriate. Forget "proving the concept", since you are on the wrong track if you don't start in the right place. Commented Mar 8, 2016 at 0:00
  • As to the basic question. ALL ( well most ) MongoDB operations essentially return a BSON object ( translated into relevant structure for the language in use ). Only .distinct() makes any attempt to return anything else ( array of values ). It is the job of your code to extract values from the returned object(s) in the way you want to use it, and not the job of the database to do such transformations. Commented Mar 8, 2016 at 0:02

1 Answer 1

3

If you have one record per domain, use the findOne method,

var hits = db.test_collection.findOne({dom:"seagoedd.org"},
                                   {hits:true,_id:false})["hits"];

This works because this method returns a single document. whereas find returns a cursor to the result list.

Note: if no records match, the findOne method would return you null. in such cases, the below would make more sense:

var record = db.test_collection.findOne({dom:"seagoedd.org"},
                                   {hits:true,_id:false})
var hits = record?record["hits"]:0;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.