Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manipulate data and then do something #7

Open
osadan opened this issue Sep 6, 2015 · 5 comments
Open

manipulate data and then do something #7

osadan opened this issue Sep 6, 2015 · 5 comments

Comments

@osadan
Copy link

osadan commented Sep 6, 2015

How I can do make update on data without chaining the command to find();

_mongo.connect.then(function() {
            _mongo.collection('friends').findOne({
                user: {
                    id: userId
                }
            }).then(function(user) {
                var result = importContacts(user,maxResults);
             // I want to save the contacts of the user at this point 

 });

how I can do that (updating the user object without calling collection("friends").find(user) again

@jonathanong
Copy link
Contributor

use .new() on an update() command?

@osadan
Copy link
Author

osadan commented Sep 7, 2015

but then I still will have to chaing the update .
I need to call a "chainable" command inside the promise callback function .

@jonathanong
Copy link
Contributor

I don't get it.

@osadan
Copy link
Author

osadan commented Sep 8, 2015

Update and new is chainable functions that you chain to collection (actually Query Object) .
means after you call object then you update him

_mongo.collection('friends').findOne({user: {id: userId}}).update({set : {data : data}});

but what if I want to process some logic based on the data I get from the colllection and then update the object with the new data let say change a flag

_mongo.collection('friends').findOne({user: {id: userId}}).then(function(friend){
if(friend.hasFriends){
//some logic
}else{
//get friends
}
});

now If I want to update the object I have to call the collection.findOne() again in order to chain the update function .

else{
//get friends
firend.hasFriends = true;
_mongo.collection('friends').findOne({user: {id: userId}}).update({set : {hasFrineds : true}});
}

I have to say that I use mongo-next as main module in my project and It is very helpful for me and saved me a lot of time and effort

@osadan
Copy link
Author

osadan commented Sep 9, 2015

I think if the collection will be added to the query result as parameter it will be possible to chain actions on the promise callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants