How to delete entry from MongoDB with AngularJS?

2,233 views
Skip to first unread message

r0bs

unread,
Nov 14, 2012, 3:59:04 PM11/14/12
to ang...@googlegroups.com
Hi!

I'm new to AngularJS and MongoDB, so the following question should be an easy one:

In this fiddle (http://jsfiddle.net/r0bs/hwP3y/19/) I created a list of things that I save to a MongoDB via $ressource. Adding new entries is no problem. In the next step I want to delete things by clicking on them. 

For now I tried to remove an element, just to see how removing elements from a MongoDB is works. By clicking any element in the list the script should delete all entries with the name "Hi". (Before I tried to delete a specific entry by its id, didn't work either.) The Code for that looks like this: 

MongoLabRessource.removename'Hi' );

How can I do the removal right?

Thank you so much in advance!

Pawel Kozlowski

unread,
Nov 14, 2012, 4:04:45 PM11/14/12
to ang...@googlegroups.com
Hey,

The author of the mongolab resource wrapper (that you are using!) here :-)

So, one of the ways (the easiest?) of deleting an item is shown in
this fiddle: http://jsfiddle.net/XxUMz/1/

$scope.removeProject = function(project) {
project.$delete(function(){
$scope.projects = Project.query();
});
}


You might found it interesting that there is a new version of this
wrapper that works with promises:
https://github.com/pkozlowski-opensource/angularjs-mongolab-promise

Here is the live CRUD demo: http://embed.plnkr.co/Y8sg4V

Hope this helps,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>



--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

rob...@gmail.com

unread,
Nov 14, 2012, 4:14:47 PM11/14/12
to ang...@googlegroups.com
Hi Pawel,

thanks a lot for your help!

I appreciate your great work with the wrapper!

Thanks again!

r0bs
Message has been deleted

rob...@gmail.com

unread,
Nov 19, 2012, 9:57:27 AM11/19/12
to ang...@googlegroups.com
Hey Pawel,

maybe you could answer another question? I decided to load the data from the mongolab-db, work on it in my app and than save the whole thing back instead of passing every change to the db.

I tried to do this by using the $save()-function (like this: project.$save();) but it didn't work [console says "405 Method not allowed"]. I would be glad if you can give me a hint how to do this right. 

Thank you so much!

r0bs

Am Mittwoch, 14. November 2012 22:04:52 UTC+1 schrieb Pawel Kozlowski:

Pawel Kozlowski

unread,
Nov 17, 2012, 10:31:17 AM11/17/12
to ang...@googlegroups.com
Hi r0bs!

Sure, would be happy to help but I'm not sure I got you here... Could
you send a jsFiddle / plunker (you can modify the one I've sent or
prepare a new one) so I can better understand what you are trying to
do?

I'm not sure if the $save fails on a particular project or on an array
of projects...

Cheers,
Pawel

On Sat, Nov 17, 2012 at 4:13 PM, <rob...@gmail.com> wrote:
> Hey Pawel,
>
> maybe you could answer answer another question? I decided to load the data
> from the mongolab-db, work on it in my app and than save the whole thing
> back instead of passing every change to the db.
>
> I tried to do this by using the $save()-function (like this:
> project.$save();) but it didn't work [console says "405 Method not
> allowed"]. I would be glad if you can give me a hint how to do this right.
>
> Thank you so much!
>
> r0bs

Tony Polinelli

unread,
Nov 20, 2012, 12:06:20 AM11/20/12
to ang...@googlegroups.com
When i've done deletes - i remove it from the array manually - so that it happens immediately - do you think this would be bad?



rob...@gmail.com

unread,
Nov 20, 2012, 4:03:40 PM11/20/12
to ang...@googlegroups.com, to...@touchmypixel.com
Hi Tony,

thank you for your solution! It works fine for smooth deleting of items from the list! Another way would be to replace the for-loop by $scope.projects.splice($scope.projects.indexOf(project), 1);.

r0bs

rob...@gmail.com

unread,
Nov 20, 2012, 4:04:23 PM11/20/12
to ang...@googlegroups.com
Hi Pawel

at first my approach was to work with the help of your awesome wrapper on the object in the MongoDBWhat works very fineBut everytime assign new item to the json-object in the mongo-db have to update the view-model to the latest version from the DBThat causes short flickering of the list in the viewperfect solution would only add the new items to the view without reloading the whole list

So my idea to prevent that flickering was to assign the whole Projects-Object from the database to $scope.projects-Object on the initial load of the pagethen work on the $scope.projects-Object by addingediting or deleting items and thenon closewrite the whole thing back to the DBEven though thats not ideal for obvious reasonsSo my question is how to save the $scope.projects-Object as Project-Objekt to the database.

Tonys solution works fine for deleting itemsbut still causes the flickering of the list after adding an item.

would be glad for any suggestion on this.

Thanks!

r0bs

Pawel Kozlowski

unread,
Nov 20, 2012, 4:27:11 PM11/20/12
to ang...@googlegroups.com
Hi Rob!

You don't have to reload the whole list each time, not at all. Nothing
stops you from updating the model in a callback after saving, updating
or deleting. All the MongoDB object got this unique _id property so it
should be easy to find objects in collections (the ones to be updated
/ deleted).

Not sure if I'm making myself clear so if you could send a jsFiddle /
plunker we could work it out.

Cheers,
Pawel

rob...@gmail.com

unread,
Nov 21, 2012, 5:27:59 AM11/21/12
to ang...@googlegroups.com
Hi Pawel,

thanks again for your answer. Sounds great! I just don't know how to induce the id_property to the function so only the new (or changed) items get loaded again. 

I prepared a fiddle with comments here (http://jsfiddle.net/r0bs/HPLc9/4/). The function for reloading should be the one in line 21.

Thank you!

r0bs
Reply all
Reply to author
Forward
0 new messages