Questions on Comparison Query Syntax

Greeting MongoDB!

I’m working on the questions for Chapter Four (Query Operators).

  1. Find all documents where the trip was less than or equal to 70 seconds and the usertype was not “Subscriber”

I notice that my queries work individually:

db.trips.find({tripduration: {$lte: 70}})
db.trips.find({usertype: {$ne: "Subscriber"}})

However, when I combine them:
db.trips.find({tripduration: {"$lte": 70}},{usertype: {$ne: "Subscriber"}} )

I receive the following error:

MongoServerError: Expression $ne takes exactly 2 arguments. 1 were passed in.

Is there a way for me to fix my syntax so that I can query documents on multiple criteria?

In the find() that does not worked you passed 2 parameters, first being {tripduration: {"$lte": 70}} and the second being {usertype: {$ne: "Subscriber"}}. The first parameter of find() is the query and the second is a projection. Both condition needs to be in the first parameter. You have an extra },{ between the end of the first condition and the beginning of the second one.

2 Likes

That worked, thanks steevej!

-Rebecca

2 Likes