4

This is my code to get the data from a collection groupname which is initialised with the name of collection. I want to iterate the data stored in doc using foreach loop.

var db = mongojs('login');

    var cursor = db.collection(groupname).find();
    console.log(cursor);

    cursor.each(function(err, doc) {
        console.log(doc._id);

        mongo.collection(doc._id + "group", function(err, collection) {
            collection.remove({"groupname": groupname});
        });
    });

I've tried doing db.collectionname.find().forEach(....), but I've got an error saying that such function doesn't exists. Please help.

2 Answers 2

6

The find() call to fetch the records from Mongo DB is asynchronous. You are trying to use the docs even before the data is available. You should have your forEach loop in the call back of the find().

`

db.collection(groupname).find({}, function(err, doc){
console.log(doc);
doc.forEach(function(err,doc){
    console.log(doc._id);
    db=mongo.collection(doc_id+"group");
    db.remove({"groupname":groupname});
});
});

`

Sign up to request clarification or add additional context in comments.

2 Comments

I tried using your code. The code executes but nothing happens. The control does not enter the function(err,doc). Any idea why?
hey I realised where i was going wrong. I was deleting the collection before it could select the data. Now i get the message on console which shows the value for doc._id as undefined.
2

Use the each() method to iterate over all the documents for the find() cursor:

// Grab a cursor
var cursor = db.collection(groupname).find();
console.log(cursor);

// Execute the each command, triggers for each document
cursor.each(function(err, doc) {
    console.log(doc._id);
    // Fetch a collection to remove document
    mongo.collection(doc._id + "group", function(err, collection) {
        collection.remove({"groupname": groupname});
    }
});

-- UPDATE --

Seeing that you are using the mongojs library after your edit, you need to pass a callback function to handle the results of the query since Node.js implements an asynchronous paradigm and almost everything is always a callback which allows your app to be non-blocking and high performing:

// connect now, and worry about collections later
var db = mongojs('login')
var groupcollection = db.collection(groupname)

groupcollection.find({}, function(err, groups) {
    if( err || !groups) console.log("No groups found");
    else groups.forEach( function(group) {
        console.log(group);
        mongo.collection(group._id + "group", function(err, collection) {
            collection.remove({"groupname": groupname});
        });
    });
});

7 Comments

Hi. I get the following error TypeError: cursor.each is not a function
@sachinhunur Can you edit your question to include the code where the db object is instantiated/initialiased?
@sachinhunur You did not mention that you are using the mongojs library hence why the above piece of code doesn't work for you.
How do i make it work with mongojs? I had to use mongojs wrapper cause find() function was not working
@sachinhunur I've updated my answer to include the mongojs implementation.
|

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.