EzDevInfo.com

mongojs

Node.js module that implements the offical mongo api Mongojs by mafintosh

collection.find mongojs synchronous callback

I'm trying to write my own wrapper for the mongojs collection.find method that should return the collection items selected by the specified query (querying isn't implemented yet, it should simply select all results). The problem is that I'm not getting back an array of results. It seems like the find method does some kind of async callback. So how can I force a synchronous call or force my script to wait?

Collection.prototype.find = function () {
    var result = new Array;
    if (Bridge.isServer) {
        db.collection(name).find(function(err, items) {
            items.forEach(function(item) {
                result.push(item);
            });
        });
    }
    return result;
}

Source: (StackOverflow)

MongoDb : How to insert additional object into object collection?

I have a document "owner" that can have "n" number of camps and camps have "instructors" and instructors have "classes". Earlier I tried accomplishing this with nested arrays (see link to my post below), however I learned that the positional operator "$" does not nest that deep. MongoDB: Adding an array into an existing array

However, I learned that the workaround would be to use object collections instead of arrays. I'm assuming that I have to use "update" with "$set" to add additional "camps", however every time I do all it does is overwrite(update) the previous camp that was inserted.

Below is the hierarchical structure I am trying to accomplish:

owner = {

    firstName: 'john',
    lastName: 'smith',
    ownerEmail: 'john.smith@gmail.com',

    camps : {

        {
            name: 'cubs-killeen',
            location: 'killeen'
        },

        {
            name: 'cubs-temple',
            location: 'temple'
        },

        instructors : {

            {
                firstName: 'joe',
                lastName : 'black'
            },

            {
                firstName: 'will',
                lastName : 'smith'
            }        
        }

    }

}

I have also been trying the following:

db.owners.update({ownerEmail:'john.smith@gmail.com'}, {$set: { camps:{ {name:'cubs-killeen'} } }})

but this throws an unexpected identifier { error.

Any help with some sample mongo commands to achieve the structure above would be most appreciated.

V/R

Chris


Source: (StackOverflow)

Advertisements

MongoJS not returning data when searching with regular expressions

Node.js, mongojs, mongodb. I am searching through a list of skills using regular expressions. Here is my server code:

var nconf = require('nconf');
var db = require('mongojs').connect(nconf.get('mongolab:connection'), ['skills', 'users']);

app.get('/api/skill', function(req, res){
    console.log('searching for ' + req.query['q']);

    var query = '{name: /' + req.query['q'] + '/i}';
    console.log('query: ' + query);
    db.skills.find(query, function(err, data){
        console.log('returning ' + JSON.stringify(data));
        if(!err){
            res.writeHead(200, {'content-type': 'text/json' });
            res.write( JSON.stringify(data) );
            res.end('\n');
        }   
    });

});

I do have a value of "asp.net" in my list. Console log outputs this:

searching for .net
query: {name: /.net/i} 
returning []

I use MongoHub to connect to the same server/db, paste the statement in query field, and get back my record:

{
    "name": "asp.net",
    "description": "",
    "_id": {
        "$oid": "500b4aae14f7960e91000001"
    }
}

Any suggestions?


Source: (StackOverflow)

Mongojs - how to push to array?

I have the following code:

db.users.save({
    username: "admin",
    array: [{
            foo1: "foo1",
            foo2: "foo2"
        }, {
            bar1: "bar1",
            bar2: "bar2"
        }, {
            cell1: "cell1",
            cell2: "cell2"
        }
    ]
});

Now I want to update the array. I mean to push something into the array:

db.users.update({
    username: admin
}, {
    $set: {
        array: {
            push1: "push1",
            push2: "push2"
        }
    },


    function (err, updated) {

The update function doesn't push. So how can I push to the array, so the result will be:

[
  { foo1:"foo1", foo2:"foo2" },
  { bar1:"bar1", bar2:"bar2" },
  { cell1:"cell1", cell2:"cell2" },
  { push1:"push1", push2:"push2" }
]

Source: (StackOverflow)

get mongodb data to nodejs array?

I'm having trouble getting mongodb data into nodejs array as follow:

Test db:

{
  "supportTicket" : "viT8B4KsRI7cJF2P2TS7Pd0mfqaI5rtwf",
  "msgId" : 1379304604708.0,
  "username" : "Guest-OSsL2R",
  "message" : "hello",
  "_id" : ObjectId("5236849c3651b78416000001")
}

Nodejs:

function _getMsg(st, callback) {
    db.test.find({ supportTicket: st }).toArray(function (err, docs) {
        callback(docs);
    });
}

var nodeArr = _getMsg('viT8B4KsRI7cJF2P2TS7Pd0mfqaI5rtwf', function (res) {
    console.log(res); // --> res contain data and printed ok
    return res; 
});

console.log('return data: ' + nodeArr )  // --> However, nodeArr  is undefined.

And here is the result:

return data: undefined
return data: undefined
[ { supportTicket: 'viT8B4KsRI7cJF2P2TS7Pd0mfqaI5rtwf',
    msgId: 1379304604708,
    username: 'Guest-OSsL2R',
    message: 'dhfksjdhfkj',
    _id: 5236849c3651b78416000001 } ]
[ { supportTicket: 'viT8B4KsRI7cJF2P2TS7Pd0mfqaI5rtwf',
    msgId: 1379304604708,
    username: 'Guest-OSsL2R',
    message: 'dhfksjdhfkj',
    _id: 5236849c3651b78416000001 } ]

The question is: How can I get data from test db and assign the data to nodeArr?


Source: (StackOverflow)

Best way to move object from one collection to other in NodeJs

How to move object from one collection to other ? What is the easiest and safest way to do it? I was trying with that:

app.post('/doneList/:id',function(req, res){
    var id = mongojs.ObjectId(req.params.id);
    var oneTodo;
    db.todoList.findOne({_id: id},function(err,docs){
        db.doneList.insert(docs,function(err,doc){
            res.json(doc);
        });

    }); 
    db.todoList.remove({_id: id});
});

but it crashes when I try it.


Source: (StackOverflow)

MongoDB won't save JSON document?

Quick question, but does anyone have a clue why MongoDB won't save the following document? I'm using MongoJS in NodeJS to connect to MongoDB and using the following lines of code to save (in my JS file, the function call is underneath the variable definition). The save function doesn't save, but automatically goes to the callback function.

Any thoughts? Thanks so much :).


Edit: After adding an error log to the callback, I'm getting the following error:

{ [MongoError: error parsing element 0 of field documents :: caused by :: wrong type for '0' field, expected object, found 0: "{"Word":"Count","read":1,"letter":1,"wh":1,"urging":2,"swift":1,"approval":1,"add'l":1,"lease":1,"space":1,"fayetteville":1,"vamc":1,"vets":2,"care":1..."]
  name: 'MongoError',
  ok: 0,
  errmsg: 'error parsing element 0 of field documents :: caused by :: wrong type for \'0\' field, expected object, found 0: "{"Word":"Count","read":1,"letter":1,"wh":1,"urging":2,"swift":1,"approval":1,"add\'l":1,"lease":1,"space":1,"fayetteville":1,"vamc":1,"vets":2,"care":1..."',
  code: 9 }

db.collection.save(json_buffer, function() {
    console.log("Complete");
});

  var json_buffer = {"Word":"Count","read":1,"letter":1,"wh":1,"urging":2,"swift":1,"approval":1,"add'l":1,"lease":1,"space":1,"fayetteville":1,"vamc":1,"vets":2,"care":1,"@gnip":3,"--":3,"delivering":3,"data":3,"happy":3,"customers":3,"outrageous":1,"france":1,"sell":1,"warships":1,"putin":1,"@senatorkirk":1,"@repkinzinger":1,"@usrepkeating":1,"&amp":5,"urge":1,"strong":1,"action":3,"polling":1,"shows":1,"race":1,"close":1,"pitch":1,"pre-primary":1,"deadline":1,"goal":2,"joining":1,"@teamcavuto":1,"shortly":1,"discuss":1,"can’t":1,"continue":1,"punt":1,"debt":2,"crisis":1,"road":1,"watch":1,"opening":1,"remarks":1,"today’s":1,"senate":1,"committee":1,"hearing":1,"mcdonald":1,"nomination":1,"urged":1,"passage":1,"#summermealsact":2,"yesterday":1,"#monticello":1,"#ny--so":1,"impt":1,"expand":1,"@usda's":1,"summer":2,"nutrition":1,"program":1,"great":2,"catch":1,"high":1,"school":2,"friend":1,"john":1,"choate":1,"today":7,"family":1,"capitol":1,"hill":1,"child":1,"america":1,"wake":1,"day":1,"wondering":1,"eat":1,"nebraska":1,"communities":1,"access":1,"local":1,"tv":1,"programming":1,"introduced":1,"bill":4,"work":2,"past":1,"time":2,"congress":1,"meaningful":1,"reduce":1,"threat":1,"cyber":1,"attacks":1,"@mercnews":1,"op-ed":1,"fitting":1,"@us_sfrc":1,"passed":1,"#crpd":1,"bob":1,"dole's":1,"bday":1,"#disabilitytreaty":1,"advocate":1,"beginning":1,"#isupportcrpd":1,"senator":1,"mcconnell":2,"co-sponsors":1,"protect":2,"correctional":1,"officers":2,"daily":1,"independent":2,"#ashland":1,"#kentucky":1,"millions":1,"children":2,"recv":1,"free":1,"reduced":1,"meals":1,"year":1,"left":1,"hungry":1,"months":1,"unacceptable":1,"rt":5,"@aterkel":1,"record":1,"phone":1,"@senschumer":1,"…":1,"good":1,"meeting":1,"anne":1,"rung":1,"nominee":1,"@ombpress":1,"office":2,"fed":1,"procurement":1,"policy":1,"cc":1,"@senatehsgac":1,"@federalreserve":1,"divert":1,"leftover":1,"funds":1,"foreclosure":1,"review":1,"hardest":1,"hit":1,"fund":1,"#ri":1,"supports":1,"corrections":1,"prisons":1,"manchester":1,"pine":1,"knot":1,"ashland":1,"inez":1,"lex":1,"miami":1,"valley":1,"well-run":1,"base":1,"makes":1,"wright-patt":1,"#1":1,"airmen":1,"@gopoversight":1,"release":1,"#irs":2,"needed":1,"days":1,"confirm":1,"lerner":1,"hard":2,"drive":1,"crash":2,"provide":1,"support":3,"runaway":1,"homeless":1,"youth":1,"victims":1,"trafficking":1,"@housefloor":1,"tomorrow":1,"glad":1,"signature":1,"industry":1,"@kydistillers":1,"@repandybarr":1,"obama":1,"meant":1,"flexibility":1,"re-elected":1,"thoughts":1,"prayers":1,"affected":1,"fires":1,"tooele":1,"pray":1,"staying":1,"safe":1,"#utpol":1,"hr":1,"passes":1,"house":1,"@repderekkilmer":1,"modernize":1,"labs":1,"fyi":1,"fun":1,"activities":1,"hosted":1,"google":1,"young":1,"inventors":1,"age":1,"13+":1,"choose":1,"projects":1,"virtual":1,"field":1,"trips":1,"joined":1,"nyc":1,"workforce":2,"development":2,"hosting":1,"roundtable":1,"#bronx":1,"failure":1,"disclose":1,"timely":1,"manner":1,"destroyed":1,"critical":1,"evidence":1,"reason":2,"special":1,"prosecutor":1,"26%":1,"texas":1,"live":1,"poverty":1,"#raisethewage":1,"#honorflightact":2,"codify":1,"process":1,"tsa":1,"expedited":1,"dignified":1,"screening":1,"veterans":1,"visiting":1,"war":1,"memorials":1,"humbled":1,"join":2,"medal":1,"honor":1,"recipient":1,"staff":1,"sergeant":1,"ryan":1,"pitts":1,"nashua":1,"ceremony":1,"pentagon":1,"icymi":1,"statement":1,"halbig":1,"burwell":1,"@ecpzachevans":1,"save":1,"date":1,"mt":1,"@shrinersfest":1,"dates":1,"announced":1,"shrinersfest":1,"june":1,"25-28":1,"feat":1,"@blueangels":1,"htt…":1,"unleash":1,"america's":1,"energy":2,"abundance":1,"create":1,"#jobs":1,"economic":1,"growth":1,"affordable":1,"#yes2energy":1,"marks":1,"#100days":1,"nigerian":1,"schoolgirls":1,"kidnapping":1,"forgotten":1,"#bringbackourgirls":1,"pleased":1,"mayor":1,"@rahmemanuel":1,"taking":1,"@fly2ohare":1,"noise":1,"measure":1,"seeks":1,"pay":1,"gratitude":1,"group":1,"americans":1,"make":1,"ultimate":1,"sacrifice":1,"stopped":1,"dutch":1,"embassy":1,"offer":1,"condolences":1,"lost":1,"loved":1,"#mh17":1,"#obamafailures":1,"min":1,"#131":1,"odds":1,"drives":1,"irs":1,"claims":1,"win":1,"fl":1,"lottery":1,"times":1,"#tcot":1}

Source: (StackOverflow)

How to query for objects between date ranges

I have documents in mongodb collection that looks like below:

 "listingReservation" : {
    "reservationFromDate" : new Date("14.1.2015 00:00:00"),
    "reservationToDate" : new Date("17.1.2015 00:00:00"),
    "reservationCreatedBy" : "test@test.com",
    "reservationCreatedOn" : "",
    "reservationChangedBy" : "",
    "reservationChangedOn" : "",
    "reservationStatus" : "pending"  //status can be pedning, accepted or rejected  
  }

Now when the admin accepts a reservation, other reservation between the same date ranges and reservations which is encompassing the accepted reservation should be set to rejected status.

Now how can I find if there are already reservations objects between the new reservation dates or if there is already an encompassing reservation for the new reservation dates.

I tried below query but obviously all conditions are not working.

db1.reservationTable.update(
            {
                $and: [{
                    $or: [{

                        $or: [
                            {
                                'listingReservation.reservationFromDate': {
                                    $gte: new Date(req.body.listingReservation.reservationFromDate),
                                    $lt: new Date(req.body.listingReservation.reservationToDate)
                                }
                            },
                            {
                                'listingReservation.reservationToDate': {
                                    $gte: new Date(req.body.listingReservation.reservationFromDate),
                                    $lt: new Date(req.body.listingReservation.reservationToDate)
                                }
                            }
                        ],
                        $and: [
                            {'listingReservation.reservationFromDate': {$lte: new Date(req.body.listingReservation.reservationFromDate)}},
                            {'listingReservation.reservationToDate': {$gte: new Date(req.body.listingReservation.reservationToDate)}}
                        ]
                    }]
                },
                    {'listingBasics.listingId': req.body.listingBasics.listingId},
                    {_id: {$ne: mongojs.ObjectId(req.body._id)}},
                    {'listingBasics.listingOwner': req.user.username}
                ]
            },
            {
                $set: {'listingReservation.reservationStatus': 'rejected'}
            })

Source: (StackOverflow)

Connecting to mongoHQ with mongojs on heroku

I am trying to connect to mongoHQ from my node.js app. Here is the code I am using:

 var databaseUrl = "mongodb://fishcuss:MyPassword@alex.mongohq.com:10015/app9759558"
 var collections = ["users"]
 var db = require("mongojs").connect(databaseUrl, collections);


 exports.register = function(req, res){
   console.log("Register hit")
   var user = req.body;
   db.users.findOne({username:user.username}, function(err, users) {
     console.log(err)
     console.log(users);
     if( err || users) {
         res.statusCode = 500;
         res.end();
     }
     else {
         console.log("Inserting new user")
         user._id = uuid.v4();
         user.lists = [];
         db.users.insert(user,{},function(){
             req.session.user=user
             res.write(JSON.stringify(user), 'UTF-8');
             res.statusCode = 200;
             res.end();
         })
     } ;
 });
};

However I seem to get this error

{ [MongoError: auth fails] name: 'MongoError', errmsg: 'auth fails', ok: 0 }

Which leads me to believe that I am missing something in my connection. Anyone have a hint as to what that might be?

Thanks


Source: (StackOverflow)

If a multi update fails partially in mongodb, how to roll back?

I understand that a multi update will not be atomic and there are chances that the update query may fail during the process. The error can be found via getLastError. But what if I want to roll back the updates that have already been done in that particular query? Any method simpler than the tedious two phase commits?

For instance, let me say I have a simple collection of some users and their phone models. Now I do a multi update on all the users who have a Nexus 4 to Nexus 5 (Dreamy, isn't it?). The only condition being, all or none - so all the N5s are taken back if even one N4 user doesn't get his. Now, somehow the mongodb fails in between, and I am stuck with a few users with N4 and a few with N5. Now, as I have gathered from the net is that I can't have mongo roll back directly. If the operation failed, I will have to do a manual update of the N5 users back to N4. And if that fails too somehow, keep repeating it. Or when I have a complicated collection, I will have to keep a new key viz. Status and update it with keywords updating/ updated.

This is what I understand. I wanted to know if there is any simpler way. I assume from the comments the answer is a big no.


Source: (StackOverflow)

Callback was already called async parallel

Hi I am trying to use the Async module to retrieve two users and do some processing after they have both been retrieved however I keep getting the error message: Callback was already called. Below is the code i currently have:

app.get('/api/addfriend/:id/:email', function(req, res) {
    var id = req.params.id;
    var friendEmail = req.params.email;
    async.parallel([
            //get account
            function(callback) {
                accountsDB.find({
                    '_id': ObjectId(id)
                }, function(err, account) {
                    console.log(id);
                    if (err || account.length === 0) {
                        callback(err);
                    }
                    console.log(account[0]);
                    callback(null, account[0]);
                });
            },
            //get friend
            function(callback) {
                accountsDB.find({
                    'email': friendEmail
                }, function(err, friend) {
                    console.log(friendEmail);
                    if (err || friend.length === 0 || friend[0].resId === undefined) {
                        callback(err);
                    }
                    console.log(friend[0]);
                    callback(null, friend[0].resId);
                });
            }
        ],

        //Compute all results
        function(err, results) {
            if (err) {
                console.log(err);
                return res.send(400);
            }

            if (results === null || results[0] === null || results[1] === null) {
                return res.send(400);
            }

            //results contains [sheets, Friends, Expenses]
            var account = results[0];
            var friend = results[1];
            if (account.friends_list !== undefined) {
                account.friends_list = account.friends_list + ',' + friend;
            }
            else {
                account.friends_list = friend;
            }
            // sheetData.friends = results[1];
            accountsDB.save(
                account,
                function(err, saved) {
                    if (err || !saved) {
                        console.log("Record not saved");
                    }
                    else {
                        console.log("Record saved");
                        return res.send(200, "friend added");
                    }
                }
            );

        }
    );
});

Any help would be appreciated.


Source: (StackOverflow)

Should I use mongoose if the schema is too dynamic?

What I have understood so far is that the mongoose needs us to define a schema. But what if my schema keeps changing on a per user basis. For instance, let's say there are thousands of users of mobile phones. Each user has a different kind of offer subscriptions and what nots. New offers keep coming, and he can even choose combos of offers, creating new offers on the fly. So these offers become keys holding sub documents of various other details regarding that offer. Such a schema can't be predefined. Shall I use mongoose then? Or stick to mongojs type thin-skin wrappers and forget about mongoose's ODM capabilities?


Source: (StackOverflow)

MongoDB combining find with text search

I'm trying to filter a MongoDB collection with a .find() query and run a text search on the results to lower the cost of the query but I can't seem to be able to chain the commands.

Here's what I've tried (that doesn't work):

db.jobs.find({
    "salary.max": {
        $gte: 50000,
        $lte: 120000
    }
}).runCommand("text", {
    search: "metal"
})

I've also tried the query in the reverse order, which defeats the purpose and doesn't work either.

Is there a way to chain a .runCommand() to a .find() in MongoDB?


Source: (StackOverflow)

TypeError: Cannot read property '_id' of undefined

Hello Gurus of Node + mongodb,

I am getting error "TypeError: Cannot read property '_id' of undefined" on a simple post request to save a document to the coolection 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\mongojs\node_modules\mongodb\lib\mongodb\mongo_client.js:411
          throw err
                ^
TypeError: Cannot read property '_id' of undefined
    at Collection.save (C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\mongodb\lib\mongodb\collection.js:393:15)
    at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\index.js:203:65
    at apply (C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\thunky\index.js:16:28)
    at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\thunky\index.js:20:25
    at Db.collection (C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\mongodb\lib\mongodb\db.js:488:44)
    at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\index.js:268:7  
    at apply (C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\thunky\index.js:16:28)
    at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\thunky\index.js:20:25
    at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\index.js:300:4
    at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\mongodb\lib\mongodb\mongo_client.js:408:11
31 Aug 00:14:30 - [nodemon] app crashed - waiting for file changes before starting...

Thanks for the answers in advance. BR, chids


Source: (StackOverflow)

How to send MongoDB query result as a JSON response using express?

I'm writing an application where I use express, Node.js and MongoDB (using mongojs). I have a module db.js and a server.js, which have the snippets below.

db.js

var getUsersByCity = function(city, callback) {
    db.users.find({'city': city}).toArray(function(err, data) {
        if (err) {
            callback(err);
            console.log(err);
        } else {
            console.log(data);
            callback.json(data);
        }
    });
}

server.js

app.post("/get_users_list", function(req, res) {
    var body = req.body;
    db.getUsersByCity(body.city, res);
});

It's working because, as you can see, I'm (probably incorrectly) using callback.json(data), when I should be using callback(data). I think the db.js module should not be responsible for sending the response and I should pass res.json as the callback to my function.

The problem is: when I do things the way I consider right, I face the following error:

path_to_my_app/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/base.js:245
        throw message;      
              ^
TypeError: Cannot call method 'get' of undefined
    at res.json (path_to_my_app/node_modules/express/lib/response.js:189:22)
    at path_to_my_app/db.js:36:13
    at path_to_my_app/node_modules/mongojs/node_modules/mongodb/lib/mongodb/cursor.js:163:16
    at commandHandler (path_to_my_app/node_modules/mongojs/node_modules/mongodb/lib/mongodb/cursor.js:706:16)
    at path_to_my_app/node_modules/mongojs/node_modules/mongodb/lib/mongodb/db.js:1843:9
    at Server.Base._callHandler (path_to_my_app/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/base.js:445:41)
    at path_to_my_app/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/server.js:468:18
    at MongoReply.parseBody (path_to_my_app/node_modules/mongojs/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
    at null.<anonymous> (path_to_my_app/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/server.js:426:20)
    at EventEmitter.emit (events.js:95:17)

How to properly send the JSON response without sending the response object to my DB module?

P.S. The content of line 36 of db.js, when I make the changes, is callback(data);.


Source: (StackOverflow)