1

I'm using Studio3T to run MongoDB, but I'm a beginner. I have a publication.notes field that I use to exclude items from a report, this field is normally not used otherwise so a majority of the documents do NOT have this field populated and we have been running this code below for the report. (publication.notes is the last line)

{
$and: [
    {
        "warehouses.code": "RSDMAIN" 
    }
    ,
    {
        "warehouses.0.quantity": {
            $gt: NumberInt(0) 
        }
    }
    ,
    {
        "images.0.image": {
            $exists: false 
        }
    }
    ,
    {
        "featured_image.slug": {
            $exists: false 
        }
    }
    ,
    {
        "publication.status": {
            $ne: "discontinued" 
        }
    }
    ,
    {
        "publication.status": {
            $ne: "rejected" 
        }
    }
    ,
    {
        "product_type": {
            "$in":[
                "standard" ,
                null 
            ]
        }
    }
    ,
    {
        "publication_notes": {
            $exists: false 
        }
    }
]
}

The problem is that we have had a small percentage of documents that the publication notes does get populated, but it stays null. I would like my report to contain both the null and the does not exist documents, while still excluding the ones with actual text. Everything I try makes me either null OR DNE.

I tried playing around with "publication.notes": { $not: { $type: 2 } } , but they return the same report as "publication_notes": { $exists: false }, which makes me think that it is seeing whatever that is in the field as a string, even if it is null.

gpace
  • 13
  • 2

1 Answers1

0

Please try publication_notes : null to get both null and not exists

sample collection

> db.t44.find()
{ "_id" : ObjectId("5c3543fc01f1171d3864b924"), "a" : "asdaw" }
{ "_id" : ObjectId("5c35440201f1171d3864b925"), "a" : null }
{ "_id" : ObjectId("5c35440601f1171d3864b926") }

find result

> db.t44.find({a : null})
{ "_id" : ObjectId("5c35440201f1171d3864b925"), "a" : null }
{ "_id" : ObjectId("5c35440601f1171d3864b926") }
>
Saravana
  • 12,647
  • 2
  • 39
  • 57
  • This still only returns the not exists items. I have not narrowed down what exactly is causing the field to populate, but I'm wondering if figuring that out would help me identify how to work around it. In case there is something different about this string. – gpace Jan 09 '19 at 16:39
  • can you post a null document – Saravana Jan 09 '19 at 17:29
  • { "_id" : ObjectId("55a25122814981be6e8cb0f8"), "product_type" : "standard", "tracking" : { "model_number" : "ENK 467-880-8045BK", "sku" : "", "upc" : "", "oem_model_number" : "467-880-8045BK", "model_number_flat" : "ENK4678808045BK", "vendor_number" : "SUP-000055" ], "quantities" : { "manual" : "" }, "install_instructions" : "", "publication_notes" : "", "superseded_by" : "", "photo_quality" : "null", "notes" : "", – gpace Jan 09 '19 at 18:13
  • That looks awful, sorry. Can't figure out how to format it correctly. It also had like 350 lines that I removed so that it would fit in the comment. – gpace Jan 09 '19 at 18:14
  • its an empty string in your case, `null`, `not exists` and `empty` strings are completely different things – Saravana Jan 09 '19 at 19:00
  • If you want empty strings as well, you need write $or along with the one posted in answer – Saravana Jan 09 '19 at 19:01
  • That was it. Thank you so much! – gpace Jan 09 '19 at 20:03