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

How to use method distinct? #146

Open
bengle opened this issue Aug 11, 2014 · 2 comments
Open

How to use method distinct? #146

bengle opened this issue Aug 11, 2014 · 2 comments

Comments

@bengle
Copy link

bengle commented Aug 11, 2014

db.collection.distinct('xxx',function(err,docs){...})
I used it like this,but it seems not correct.

@mbejda
Copy link

mbejda commented Dec 15, 2014

The only way I got distinct working with mongo skin is by doing this :

var store = mongo.db(connectionString, ackOptions);
store.open(function(err,resp){
     resp.collection(this.options.collection, {strict: true}, function(err, myCollection) {
        myCollection.distinct('tag',function(err,res){
            console.log(err)
            console.log(res)
        })
    });
})

@jmartorell
Copy link

Distinct function goes via native MongoDB driver, no code is added by MongoSkin AFAIK.
distinct returns an array of keys, but only that.

distinct(key, query, callback)

You can use the result at the callback as a new query.

Imagine you want to get all the authors who greeted at least once in their posts:

var collection = this.db.collection('posts');
    collection.distinct( "author", {
      message: /^hi/
    }, function(error, keys) {
        collection.findItems({
            "author": { $in: keys}
        },
            function (error, results) {
              if (!error) {
                console.log(results)
              }
            });
    });

This works for me, I hope this issue can be closed.

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

3 participants