0

I have an array inside a collection in mongodb as per below.

{
"_id" : ObjectId("53dbb05fa976627439d43884"),
"employee" : [ 
    {
        "date" : "1986-03-10"

    }, 
    {
        "date" : "1986-12-11"

    }
    ]
    }

Now I want to find all distinct date and output it to array in nodejs format.

var collection = db.collection('employee');
collection.distinct('employee.date').toArray(function(err, docsa) {

console.log(docsa);

});

};

The problem is I will receive the following error, but those collection.distinct command can be run on mongodb.Anyhow I can avoid the error ?

 TypeError: Cannot call method 'toArray' of undefined 

Or is there any way I can use collection.find() to get the same output?

1 Answer 1

1

you try collection.distinct('employee.data'), on a collection called 'employee'. I believe you want :

collection.distinct('date')
Sign up to request clarification or add additional context in comments.

1 Comment

opss Yes..typo error..its corrected..basically it's still the same error wrt to collection.distinct

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.