2

I want to query a document that looks like -:

{
    fname : "Michael",
    fields: [
                {
                    field:"[A,B,C,D]"
                },
                {
                    field:"[A,C,D]"
                }
            ]
}

Now from this document I want the resultant to be :

    {
            fname : "Michael",
            fields: [
                        {
                            field:"[A,C,D]"
                        }
                    ]

    } 

I am using this query to get the result, but I am getting error:-

db.mycollection.aggregate([
    {
        $project : {
            _id : 0,
            fields : { $filter : { input : "$fields", as : "f", cond : {"$$f.field" : {$nin : [/B/]}}}}}
        }
    }
])

Can anyone help in resolving the error ?

1 Answer 1

1

You can use $indexOfBytes to check the existence of a single character (returns -1 otherwise):

db.collection.aggregate([{
    $project : {
        _id : 0,
        fields : { 
            $filter : { 
                input : "$fields", 
                as : "f", 
                cond : { $eq: [ { "$indexOfBytes": [ "$$f.field", "B" ] }, -1 ] }
            }
        }
    }
}])

Mongo Playground

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

Comments

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.