Domanda

I have the following SELECT statement for SQL.

SELECT TransAmount FROM STOCK WHERE TransAmount between 100 and 110;

However, this statement generates an error from querymongo.com. It says "Failure parsing MySQL query: Unable to parse WHERE clause due to unrecognized operator ". I assume it is talking about the between clause.

Correct me if I'm wrong, but does this SQL statement do the exact same thing as the one above?

SELECT TransAmount FROM STOCK WHERE TransAmount > 100 and TransAmount < 110;

This statement generates the following MongoDB code.

db.STOCK.find({
    "TransAmount": {
        "$gt": 100,
        "$lt": 110
    }
}, {
    "TransAmount": 1
});

It looks like MongoDB doesn't have a 'between' operator. Does MongoDB handle selection within ranges with a different keyword, or do you have to set it up like so $gt/%lt?

È stato utile?

Soluzione

Between is just a shortcut (a sort of symlink) to your second query, I guess it makes life easier.

MongoDB has not yet implemented such a shortcut, I have looked around a bit for a JIRA declaring someone wants such an operator however, no luck.

The one and only way of doing ranges in MongoDB is to use $gt and $lt (you could count $in etc but that is a different kind of range, not what your looking for).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top