Linq: Command find failed: $and/$or/$nor must be a nonempty array

477 views
Skip to first unread message

Michael Krog

unread,
Nov 19, 2018, 7:23:46 PM11/19/18
to mongodb-user
I am trying to create a dynamic Linq expression for searching in Mongo. But no matter what I do it fails.

I do it like this:
Expression<Func<IndexItem, bool>> predicate = e => e.Title.Contains(text) || e.Description.Contains(text);
var q = collection.AsQueryable<T>().Where(predicate);
List list=q.toList()


But I always get this error:
MongoDB.Driver.MongoCommandException: Command find failed: $and/$or/$nor must be a nonempty array.
   at
MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)
   at
MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocol[TResult](IWireProtocol`
1 protocol, CancellationToken cancellationToken)
   at
MongoDB.Driver.Core.Servers.Server.ServerChannel.Command[TResult](ICoreSession session, ReadPreference readPreference, DatabaseNamespace databaseNamespace, BsonDocument command, IElementNameValidator commandValidator, BsonDocument additionalOptions, Func`1 responseHandling, Boolean slaveOk, IBsonSerializer`1 resultSerializer, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
   at
MongoDB.Driver.Core.Operations.CommandOperationBase`1.ExecuteProtocol(IChannelHandle channel, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.CommandOperationBase`
1.ExecuteProtocol(IChannelSource channelSource, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken)
   at
MongoDB.Driver.Core.Operations.ReadCommandOperation`1.Execute(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindCommandOperation`
1.Execute(IReadBinding binding, CancellationToken cancellationToken)
   at
MongoDB.Driver.Core.Operations.FindOperation`1.Execute(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperation[TResult](IReadBinding binding, IReadOperation`
1 operation, CancellationToken cancellationToken)
   at
MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
   at
MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation`1 operation, CancellationToken cancellationToken)
   at
MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`
1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`
1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
   at
MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.AsyncCursorSourceEnumerableAdapter`
1.GetEnumerator()
   at
System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at
System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)

Fabio

unread,
Nov 20, 2018, 4:44:14 PM11/20/18
to mongod...@googlegroups.com
you use regex and builders, is faster.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/ec25eb34-ba2b-4a35-80a0-8425c115ca71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Cochran

unread,
Nov 20, 2018, 6:18:11 PM11/20/18
to mongodb-user
Hi!

You should post your MongoDB version, your driver version, and a few example documents.

Can you state in English exactly what you want to query for? Often, if you can precisely write down in every day language what you want to do, it is an enormous help in coding the actual query. 

Thanks so much

Bob

Michael Krog

unread,
Nov 23, 2018, 9:48:44 AM11/23/18
to mongodb-user
Hi.

I am running Mongo 3.6 (from the official docker image at https://hub.docker.com/_/mongo/).
I have tried with 2 driver versions: 2.5.1 and 2.7.2. Same result.

The following is an example of a document:
{
    "_id" : ObjectId("1234567890abcdef12345678"),
    "CreatedBy" : null,
    "CreatedDate" : Date(-62135596800000),
    "LastModifiedBy" : null,
    "LastModifiedDate" : ISODate("2018-11-19T14:00:11.021Z"),
    "ItemTypeId" : "5bf2c1ebef99252d6dc4f849",
    "Title" : "Item title",
    "Description" : "This is a description",
    "Permissions" : {},
    "Attributes" : {
        "gender" : "Female",
        "dateOfBirth" : "c. January 1, 1912"
    },
    "Status" : 0,
    "Notes" : [ 
        {
            "Title" : "A title",
            "Content" : "Some content",
            "_id" : "note_1"
        }, 
        {
            "Title" : "Another title",
            "Content" : "Some other content",
            "_id" : "note_2"
        }
    ]
}


In plain english, I want to be able to search for documents in mongo using any predicate of type System.Linq.Expressions.Expression<Func<IndexItembool>>..
I am building a REST api using .NET Core MVC that accepts parameters for building the predicate. For this to work, I need to be able to query documents using a System.Linq.Expressions.Expression<Func<IndexItembool>> predicate. But I am unable to get even the most simple and static predicate to work without getting the mentioned error. :-(

- Michael

Michael Krog

unread,
Nov 23, 2018, 9:49:35 AM11/23/18
to mongodb-user
Thanks.

But it won't help me build dynamic queries which is what I am aiming at!

- Michael

Robert Cochran

unread,
Nov 23, 2018, 2:29:19 PM11/23/18
to mongodb-user
When I look up the Docker tags for mongo, it seems to me that you are using either the 3.6.9-stretch tag or the 3.6-windowsservercore tag. Which exact tag are you using, and what is your host operating system?

Thanks so much

Bob

Robert Cochran

unread,
Nov 24, 2018, 10:57:29 PM11/24/18
to mongodb-user
I've spent a big part of my holiday period learning Docker. How did you get .NET Core installed? Did you customize a Dockerfile and build from it, and then run the resulting Docker image? How are you persisting the MongoDB data?

Thanks so much

Bob

Robert Cochran

unread,
Nov 26, 2018, 5:15:36 PM11/26/18
to mongodb-user
I will say this: it is extremely difficult to take a mongo/3.6 Dockerfile from e.g. GitHub, and customize that Dockerfile on your local machine, and then build a new container that will actually work. The logic for verifying the gpg keys is flawed because it is trying to extract keys from ha.pool.sks-keyservers.net instead of ipv4.pool.sks-keyservers.net and results in strange gnupg errors that then kill the entire chain of operations in that Docker command. Sometimes the logic works, sometimes not. Then even if you build it correctly, a 'docker run' on the container can fail due to permission errors on the one COPY command in this Dockerfile. The error messages are pretty unfriendly and don't hint to the cause. (Maybe one needs to chown the 'docker-entrypoint.sh' script so that root owns it?) In total, the Dockerfile is flawed and I'm not quite sure how the original developer was able to build and test a container successfully from it. It could be that my host machine (Ubuntu 18.04.1 LTS) has some quirks to it that the original developer didn't have to work around. 

I recognize that I don't know much about Docker -- indeed, this weekend was a deep dive for me in coding Dockerfiles. And researching gnupg, too.

So to test out Michael's problem I think a Vagrant box might help. I could be just way too accustomed to Vagrant and VirtualBox, but I like them...

Bob



On Friday, November 23, 2018 at 9:48:44 AM UTC-5, Michael Krog wrote:

Robert Cochran

unread,
Dec 1, 2018, 1:30:49 PM12/1/18
to mongodb-user
Michael -- can you provide a Dockerfile and the complete query you are doing through C#? 

Thanks

Bob
Reply all
Reply to author
Forward
0 new messages