0

Im trying to query mongo, and query's based on a specific field, such as a username or country, it returns only that match. I can get the data i want on a get request, but whenever I try to query by specific field it breaks. It will return this error.

my query string : localhost:4002/users?firstName=Mark

err: MongoError: Can't canonicalize query: BadValue Unsupported projection option: firstName: { $match: "Mark" }

  app.get('/users', (req,res) => {
    co(function*(){
      var dbSocial = yield MongoClient.connect(dbSocialUrl);

      let query = {

          firstName:1,
          lastName:1,
          createDT:1,
          "_mailAddress.country":1,
          ownerId:1,
          userName:1
      }
      //
      if (req.query.firstName) {
        query["firstName"] = {
          $match: req.query.firstName || '',
        }
      }

      console.log('query',query);

      //get users
      var users = yield dbSocial
        .collection('user')
        .find({},(query))
        .limit(100)
        .toArray();
     })

2 Answers 2

0

Problem was with my query object.

let query ={}

Works

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

Comments

0

$match is a Aggregation Pipeline Operators that takes a document that specifies the query conditions. It takes an object as a query.

{ $match: { <> } }

You should put the fields that you want match, which is, in your case, name. So build the query like,

db.collection_name.aggregate({$match:{'name':'Mark'}})

Comments

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.