How to specify multiple $or operators in an IMongoQuery document.

190 views
Skip to first unread message

Xu Guoming

unread,
Mar 6, 2014, 11:14:29 PM3/6/14
to mongo...@googlegroups.com
Try to construct a IMongoQuery document as below but get hit by error "Duplicate element name '$or'.". Any advice, please? 

            IMongoQuery query = new QueryDocument { 
                { flag, true }, 
                { $or, new BsonArray{
                    new BsonDocument{{startDate, BsonNull.Value}}, 
                    new BsonDocument{{startDate, new BsonDocument{{$lt, DateTime.Now}}}}
                }},
                { $or, new BsonArray{
                    new BsonDocument{{endDate, BsonNull.Value}},
                    new BsonDocument{{endDate, new BsonDocument{{$gt, DateTime.Now}}}}
                }},
            };

The query is trying to return documents whose flag is true, plus current time falls in between startdate and enddate, or if documents have no start and end dates defined, they should also be returned. 

Appreciate your advice. 

Asya Kamsky

unread,
Mar 9, 2014, 5:26:26 AM3/9/14
to mongo...@googlegroups.com
Hi,

If you need to combine two $or conditions where both are required to return true, then you need to have both of them under $and clause.

{$and : [ {$or:[ first $or conditions] }, {$or:[ second $or conditions ]} ] }

By the way, this group is for developers of MongoDB - you probably want to direct questions like this one to mongodb-users Google Group.

Asya



--
You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-dev...@googlegroups.com.
To post to this group, send email to mongo...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-dev.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages