0

I'm writing my own CLI, which performs some actions against a MongoDB database using mongoose.

Everything works except for the fact that as soon as the query object is something other than {}, I get no results despite documents existing. See below for an example.

const program = require('commander');
const mongoose = require('mongoose');
const jsonParser = require('../helpers/json-parser');

program
  .command('get <collection>')
  .option('-q,--query <query>', 'MongoDB query object', jsonParser)
  .option('-qo,--query-options [query-options]', 'MongoDB query options object', jsonParser)
  .description('Returns documents from a MongoDB collection')
  .action(action);

program.parse(process.argv);

function action(collection, options) {

  let db;

  mongoose.connect('<my-mongo-url>', { useNewUrlParser: true }).then(() => {

    db = mongoose.connection.db;

    console.log(options.query);
    return db.collection(collection).find(options.query, options.queryOptions).toArray().then((docs) => {
      console.log(docs);
      mongoose.disconnect();
    });
  })
  .catch((err) => {

    if (db) {
      mongoose.disconnect();
    }
  });
}

Running $ my-cli get users -q '{}' gives the following result:

[ { _id: 5cfe593b3bb7cb82644651f0, email: '[email protected]', createdAt: 2019-06-10T13:20:59.784Z, updatedAt: 2019-06-10T13:20:59.784Z, role: 'user', __v: 0 } ]

Running $ my-cli get users -q '{"_id": "5cfe593b3bb7cb82644651f0"}' gives the following result:

[]

Running $ my-cli get users -q '{"limit": 1}' gives the following result:

[]

Running $ my-cli get users -q '{"skip": 0}' gives the following result:

[]

I even tried doing db.collection(collection).find({_id: "5cfe593b3bb7cb82644651f0"}) directly in the code but it still only gives me an empty array.

I expect all of these queries to output the same thing but they don't. Why is this?

2 Answers 2

1

The _id in mongoDB is not string, actually it's an instance of ObjectId. So maybe you should go db.collection(collection).find({_id: ObjectId("5cfe593b3bb7cb82644651f0")}).

As for '{"limit": 1}' and '{"skip": 0}', I understand that you want to Modify the Cursor Behavior to limit or skip the search results.

However, the db.collection.find receive two params: query and projection, so passing '{"limit": 1}' or '{"skip": 0}' as the find secondary param maybe not what you want.

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

Comments

1

You are using Mongoose but not using Schemas and querying directly to MongoDB (so I don't understand the purpose of using it.). This way, you should compare the _id field with an ObjectId:

db.collection(collection).find({_id: ObjectId("5cfe593b3bb7cb82644651f0")})

or

$ my-cli get users -q '{"_id": ObjectId("5cfe593b3bb7cb82644651f0")}'

If I'm not wrong, the other queries are not running because you are passing options as query filters and you should use the -qo flag:

$ `my-cli get users -qo '{"limit": 1}'

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.