0

I'm using loopback js for backend rest api. I'm switching from Mysql to Mongodb.

I have been using query filters like these:

{filter:{where:{fieldName:null}}}

This works fine with mysql but when switching to mongodb it dosen't work. https://docs.mongodb.com/v3.2/tutorial/query-for-null-fields/ It is supported as document says. Am I missing some configurations for loopback?

My goal is to change mysql to mongodb without updating any queries.

2 Answers 2

2

The connector is turning your query into command=find [ { fieldName: { '$type': 10 } },

You can add this to your datasource object to use the extended operators and regain your functionality.

"allowExtendedOperators": true

ex { "where": { "or": [ { "name": null }, {"name": { "exists": false } } ] } }

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

3 Comments

With this solution I need to still update my queries for api. No I just want to switch the database without updating queries.
I agree it's a suboptimal solution, but a cursory google search doesn't find a way to force the mongdb : equality operator in case of null. You could also enforce field creation in mongodb and keep using "name": null. This might be a necessary consequence of switching from a relation database to a document store.
Yep, if I understood right. I could add. "name": { "type": "string", "required": true, "default": null }, To the model defination. default as null
-1

You do not need the "where" when querying in MongoDB.

Can you try querying this instead?

db.coll.find({fieldName: null})

2 Comments

Loopback has a different API to MongoDB. Queries are made though it's API and passed through to a connecting service to the database on the back end. This does not answer the question.
Thats true. Loopback has a different api.

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.