1

I need to know how to use where in MongoDB queries using Java Driver.

SELECT COLUMN1, COLUMN2 WHERE COLOUM3 = 'KeyWord';

I have a key Name and its value, and i want to find some other key/value. I think i can use $where for that. But, i dont know its syntax in java.

How shall i use where in my mongo queries?

Any suggestions would be appreciative!!!!

Thanks!

2 Answers 2

3

You shouldn't generally need to use $where ...

You should be using the "document-style query" find()

Basically find() is MongoDB's version of where ...

If you have a collection called mycollection and a attribute called attribute1 (this is exactly like your COLUMN1)

So to get results like this SQL query ...

SELECT * WHERE COLOUM3 = 'KeyWord';

Via the MongoDB shell, you'd use find() like so ...

> db.mycollection.find({attribute1:"KeyWord"})

You really shouldn't need to use $where for most any normal mongodb query.

Lots more help here: http://www.mongodb.org/display/DOCS/Querying

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

Comments

1

There is a site called www.querymongo.com that translates SQL syntax into MongoDB syntax. It's really useful for cases like this.

In the case of the example above:

SQL:

SELECT * FROM collection WHERE COLOUM3 = 'KeyWord';

Becomes this in MongoDB:

db.collection.find({
    "COLOUM3": "KeyWord"
});

1 Comment

This appears to be undisclosed self-promotion. Please be aware that this is against the rules on Stack Overflow and will quickly get you marked as a spammer if continued.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.