How to search for Id in mongodb

Asked

Viewed 646 times

0

How to search in Mongo with condition parameters, type:

Collection.find({userId}, {done: true}).fetch();

This way I wrote I want to return all the documents that have the done true, doing a search in the User id.

I have at Collection one userid, _id of the Collection, and the field done

I’m using Meteor and grahql, this is mine resolve

 Query: {
    async taskCompleted(obj, args, {userId}) {
        return await Tasks.find({userId}).fetch();
    }
}

In this code snippet, it returns all tasks of the user in question

1 answer

1


The correct denotation would be.

var user_id = "as23df56hg";

//retorna um array de Tasks do respectivo query
var tasks = Tasks.find({ "userId" : user_id, "done" : true }).fetch();

You can also use the findOne

var user_id = "as23df56hg";

//retorna a primeira Task do respectivo query
var task = Tasks.findOne({ "userId" : user_id, "done" : true });

Important

How to use organization selectors

How to use field selectors

Browser other questions tagged

You are not signed in. Login or sign up in order to post.