0

I have a MongoDB object like this:

[ 
   { poster_ip: '127.0.0.1',
    post_body: 'example',
    _id: 54fc1f7808bac08f6d25f24b,
    __v: 0 },

  { poster_ip: '127.0.0.1',
    post_body: 'example',
    _id: 54fc1f7808bac08f6d25f24a,
    __v: 0 } 
]

how can i add an extra element to every nested array so it would be like this:

[ 
   { poster_ip: '127.0.0.1',
    post_body: 'example',
    _id: 54fc1f7808bac08f6d25f24a,
    __v: 0,
    newName: NewValue },

  { poster_ip: '127.0.0.1',
    post_body: 'example',
    _id: 54fc1f7808bac08f6d25f24b,
    __v: 0,
    newName: NewValue },
]

I tried this but it didn't work:

for(var i=0; i<JSONobject.length; i++){
    JSONobject[i].newName= NewValue;
}

I'm using node.js, the object is the result i get from a mongodb query.

Here is the function:

exports.Posts = function(UserID, PostID, callback) {

                Post.find( { post: PostID } , function(err, results) {

                var r = results.toObject(); //here i get error 'has no method toObject'

                for (var i=0; i<r.length; i++){
                    r[i].newName = "NewValue";
                }

                console.log(r);
                    //i don't see any changes here with console.log

                    return callback(null, results);
                });
}

EDIT:

After a little more research i found out that the returned Mongoose Document isn't a JSON object, so i'm trying now to convert it to an object.

13
  • 2
    Iterating based on JSONobject.length and changing results[i]? Commented Mar 7, 2015 at 16:24
  • There are serious semantic problems making it hard to follow you. Is JSONobject a Javascript array ? Is it the literal you first write in your question ? Then what's results ? Commented Mar 7, 2015 at 16:25
  • The for loop you posted looks fine. What makes you think it didn't work? Commented Mar 7, 2015 at 16:25
  • 1
    @Pointy it didn't work because i checked the results after the loop. Commented Mar 7, 2015 at 16:28
  • @dystroy oh, sorry for that, the previous 'results' is the JSON object. I believe that is a javascript array, correct me if i'm wrong. Commented Mar 7, 2015 at 16:30

2 Answers 2

1

I found the answer!!

We need to tell Mongoose that all we need is a plain JavaScript version of the returned results by using lean() in the query chain.

That way Mongoose skips the step of creating the full model instance and we directly get a results we can modify

Like this:

exports.Posts = function(UserID, PostID, callback) {

                Post.find( { post: PostID } , function(err, results) {


                        for (var i=0; i<results.length; i++){
                            results[i].newName = "NewValue";
                        }

                        console.log(results);

                        return callback(null, results);

                }).lean();
}
Sign up to request clarification or add additional context in comments.

1 Comment

can you believe this.... this was just what I was looking for. JS/Mongo can behave so strange...
0

I'm not sure why a for loop would fail here, because it seems fine to me. But try this:

results = results.map(function(item) {
    item.newName = "NewValue";
    return item;
});

This would create a new array where each original item is appended with the new value.

(As a rule of thumb, using .forEach(), .map(), .filter(), .reduce(), .every() and .some() is almost always superior to for when looping over an array)

6 Comments

Sorry, didn't work either. I'm starting to believe that my object isn't a JSON object.
The for loop is indeed fine, something else is wrong. Maybe a stupid mistake of mine, i don't know, i'll do my best to find out.
the console.log( Object.prototype.toString.call(results) ); returns object Array
It's possible that the array is an actual array, but the objects are not plain objects. But in that case, it's impossible to answer without a proper reproducible test case.
however, if i do this results.newName = "NewValue"; it works but it adds an element outside of the previous arrays.
|

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.