Mongo distinct query returns several records one of which is "". But find null fails to find it - mongodb

I ran a distinct query on a collection. The query syntax:
db.Collection.distinct("dict.field")
I got a set of results - one of which was "" (null).
I then tried to find the record that had a null value for the field in question:
db.Collection.find({"dict.field": null})
To my surprise, no record was found.
No indexes are set on this collection other than _id.
The field in question is a dictionary.
What am I missing?

You should look for db.Collection.find({"dict.field": ""}) instead. Null and String ("") are considered different datatypes.
https://docs.mongodb.com/manual/reference/bson-types/

Related

MongoDb database Filter is not working in Compass

When I query all the records, i get the results as shown below.
When i copy an ID from the result and add it in the filter, no result is found.
Here is the example filter from documentation
What am i doing wrong? mongo db got to be kidding me :/
You are not providing the data type with the query. Since your _id field is ObjectId type and you are comparing as string that is why it is not working.
Change your filter condition and convert your string id to ObjectId type.
{_id: ObjectId('yourId')}
MongoDB provides an automatic unique identifier for the _id field in the form of an ObjectId data type.

MongoDB Sort Operation returning single value for the field

I was trying to sort the _id field desc and return only one max value for it but having issue around it
below one is returning all the fields
db.people.find().sort({_id:-1}).limit(1)
below one is retuning multiple values for the field
db.people.aggregate({$project: { _id:1}},{ $sort:{_id:-1}})
Try the below query
db.MyColl.find({},{_id:1}).sort({_id:-1}).limit(1)

MongoDB sort by only exists entry, key with value first and key with null or not exists last

I have a collection of 15000 documents. Some documents have sr_no with numeric values and other with absent of sr_no.
Now i want to get entries like all documents comes first which has sr_no with asc then all others.
I tried .find().sort({sr_no:1}) but it return all null entries first then asc with sr_no.
This question seems too close with duplicate. But slightly defer
with numeric key.
I answered it with hack below.
I used a dirty hack for this.
MongoDB doc says that they have priorities for sorting as posted below image.
So when i sort with asc then it sort first all null (empty key consider as null) entries then sort numeric entries.
What is hack here ?
Store sr_no : "" with empty string default.
Now it will sort first numeric values then string.

Write empty strings to MongoDB Output - Pentaho

when I try to write some values into my mongodb output in Pentaho, I would like null values of string fields to be translated to empty strings. Instead the key itself is not appearing in the mongo database. For example, if my field 'name' has a null value or a missing value, then I would like 'name':'' to appear in my mongo collection. Can anyone help me with this issue ?
Use the If field value is null step to convert null values to empty strings. To actually store the empty string in mongo set KETTLE_EMPTY_STRING_DIFFERS_FROM_NULL in kettle.properties
KETTLE_EMPTY_STRING_DIFFERS_FROM_NULL=Y
Another solution is to create a schema in mongo, to have default values, like #zydcom says in the comments.

How do I include 'undefined' in the array returned by mongodb's distinct() operator when the field does not exist?

I would like to get an array of distinct values of a certain field in a mongodb collection. However, the field is optional and if any document does not have the field, I want the array to include the value 'undefined' (or null or any indication). The distinct operator seems to ignore any documents that do not have the field, rather than include 'undefined' in the array of distinct values. Does anyone know how I can override this behavior?
Documentation for the distinct operator: http://docs.mongodb.org/manual/reference/method/db.collection.distinct/
I am getting around this now by making a second db call that counts the items with this field equal to undefined, but I would like to do it in one call.
You can do this using aggregation framework:
db.collection.aggregate(
{$group:{_id:"$myField"}}
}
It will include null value if any documents don't have the field, or have it as null value.
I really don't see a way to do what you're describing in one call.
However, to optimize what you're doing now:
I think you're doing something like:
db.myCollection.count({$not: {$exists: myfield}})
If your collection is large, this can get slow. You might consider
db.myCollection.findOne({$not: {$exists: myfield}})
If no documents exist without "myfield", it will return null.