Marco
Marco

Reputation: 656

Invalid query in mongo c#

I've this query:

string jsonquery = "{
$and : [{
        WordsData : {
            $elemMatch : {
                UserId : ObjectId('57a87f5cc48933119cb96f93'),
                UserId : ObjectId('57a87f5cc48933119cb96f94')
            }
        }
    }, {
        WordsData : {
            $not : {
                $elemMatch : {
                    MatchType : 2
                }
            }
        }
    }
]
}"

if i execute it in a mongo console works correctly!
I'm trying to run this query in a c# program. Running the same json query i got the following error:

BsonDocument doc = BsonSerializer.Deserialize<BsonDocument>(jsonquery);
[Error] --> {"Duplicate element name 'UserId'."}

Why doesn't works in c# ?

Upvotes: 1

Views: 94

Answers (1)

helmy
helmy

Reputation: 9507

You need to nest multiple $elemMatch inside of a $and expression. See this this thread for an example.

Think of JSON as a map -- if you have duplicate keys they are going to override each other and cause errors and/or unexpected results.

Upvotes: 1

Related Questions