0

Basically i want to insert into my Mongodb collection called "Events" an array of objects called "Events" with an id for each entry into the array.

This is the result i want to get in json:

{
    "events" : [
        {
            "_id" : ObjectId("53a99cc608ad49712a830081"),
            "eTitle" : "Freshers Fair",
            "eDesc" : "Bring some friends with you oh wait, you have non ha !",
            "eDate" : "2014-06-19 11:20",
            "eLink" : "http://fair.com",
            "eEvent" : "NEFS"
        },

        {
            "_id" : ObjectId("53a99cc608ad49712a830082"),
            "eTitle" : "Blahh",
            "eDesc" : "Blah fdinidf !",
            "eDate" : "2014-06-19 11:20",
            "eLink" : "http://google.com",
            "eEvent" : "NEFS"
        }
    ]
}

So far this is the result i have:

[
 { 
   "_id":"53a9b5ed745363432d823d7a",
   "eTitle":"jrnfdeiujn rd",
   "eDesc":"grfdsreds",
   "eDate":"2014-07-05 22:33",
   "eLink":"reser",
   "eEvent":"Victoria Center"
 },
 { 
   "_id":"53a9b771745363432d823d7b",
   "eTitle":"Hello worlds",
   "eDesc":"blah",
   "eDate":"2014-07-20 22:33",
   "eLink":"http://google.com",
   "eEvent":"social"
 }
]

This is how i insert data with node.js:

// Set our collection
    var collection = db.get('Events');

    // Submit to the DB
    collection.insert({
        "eTitle" : eTitle,
        "eDesc" : eDesc,
        "eDate" : eDate,
        "eLink" : eLink,
        "eEvent" : eEvent
    }, function (err, doc) {
        if (err) {
            // If it failed, return error
            res.send("There was a problem adding the information to the database.");
        }
        else {
            // If it worked, set the header so the address bar doesn't still say /adduser
            res.location("eventlist");
            // And forward to success page
            res.redirect("eventlist");
        }
    });

So please i how do i make the format look this the first json format i provided. sorry for the nooby question, just started learning node ! Thanks alot

UPDATE

To post Events:

router.get('/eventlist', function(req, res) {
var db = req.db;
var collection = db.get('Events');
collection.find({},{},function(e,docs){
    console.log(docs);
    res.send(docs);
   // res.render('eventlist', {docs: JSON.stringify(docs), title: 'Test'});
}); 

});

8
  • Doesn't it already look like that? Your current JSON is within a collection called Events\ Commented Jun 24, 2014 at 17:44
  • @tymeJV Thing is that i am currently loading the json object in ios and the format has to look like the first one. :( Commented Jun 24, 2014 at 17:46
  • Your Events collection wont spit back the collection name as the first property - why not just manually add it when you get the data? var events = { Events: data } Commented Jun 24, 2014 at 17:47
  • yea that exactly what i'm looking for. Please where do i add that ? @tymeJV Commented Jun 24, 2014 at 17:50
  • Post your call to get all the Events and ill post an answer Commented Jun 24, 2014 at 17:50

1 Answer 1

2

You can do this:

collection.find({},{},function(e,docs){
    var doc = { Events : docs };
    console.log(doc);
    res.send(doc);
    // res.render('eventlist', {docs: JSON.stringify(docs), title: 'Test'});
});
Sign up to request clarification or add additional context in comments.

2 Comments

THANKS ALOT !!!!! @tymeJV You are a beast Lol ! could you please recommend any tutorial ? Thank you sooooooo much !
@Pizzy213codes -- Wish I had a tutorial to recommend - just keep playing around with Mongo and Node and it'll be second nature in no time

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.