Query with In on a int-array

392 views
Skip to first unread message

TM

unread,
Jun 19, 2012, 4:53:38 AM6/19/12
to mongodb-csharp
Hi,

I have a document like this in mongo:

{
"Acc" : [1],
"_id" : 1005
}

With Query I could do something like this :

Query.In("Acc", accommodationTypeIds.Select(a => new BsonInt32(a))

where accommodationTypeIds is a List<int>.

I'm trying to do something like this with the QueryBuilder but I'm
stucked.

I tried something like

ElemMatch(a => a.AccomodationIds, q => q.In(c => c,
accommodationTypeIds))

but this doesn't work.

Could you help me with this?

Greets

craiggwilson

unread,
Jun 19, 2012, 10:06:28 AM6/19/12
to mongodb...@googlegroups.com
What version of the driver are you using?

TM

unread,
Jun 19, 2012, 11:16:03 AM6/19/12
to mongodb-csharp
I think I have the latest version :
Revision: 038ec795c6dd69546d7f6a2f09dcf7b909958330
Author: rstam
Date: 15.06.2012 23:16:41

craiggwilson

unread,
Jun 19, 2012, 4:34:00 PM6/19/12
to mongodb...@googlegroups.com
Wow, you are on the bleeding edge.  What is in master hasn't yet been released.  It turns out that this not being possible is an oversight that will get fixed before releasing.  We'll also scan for other areas that are like this.  

Thanks for the report.

Robert Stam

unread,
Jun 19, 2012, 4:37:29 PM6/19/12
to mongodb...@googlegroups.com
Or is there some general solution that turns arrays into scalars for purposes of constructing these queries?

I know this isn't legal C#, but this is the idea:

Query<User>.EQ(x => x.AccomodationIds[*], 1) // where * represents any element

craiggwilson

unread,
Jun 19, 2012, 4:47:14 PM6/19/12
to mongodb...@googlegroups.com
We'd have to support that in the BsonSerializationInfoFinder...  Which we can do, but then it would look kinda weird.  Your [*] would be .First() or .Single()...

Robert Stam

unread,
Jun 19, 2012, 4:57:11 PM6/19/12
to mongodb...@googlegroups.com
More like .OneOf()

which we would have to define. First or Single don't sound right, they already have defined meanings.

craiggwilson

unread,
Jun 19, 2012, 5:03:29 PM6/19/12
to mongodb...@googlegroups.com
Agreed...

Robert Stam

unread,
Jun 19, 2012, 5:19:09 PM6/19/12
to mongodb...@googlegroups.com
Man, MongoDB's handling of array fields in queries is really confusing me. I always thought that when the target field was an array that the document would match if any of the array elements matched. But apparently that's not always true:

> db.test.find()
{ "_id" : ObjectId("4fe0ebac8ee0adc8c442fa02"), "colors" : [ "blue", "black" ] }
{ "_id" : ObjectId("4fe0ebbd8ee0adc8c442fa03"), "colors" : [ "yellow", "orange", "red" ] }
>
> db.test.find({colors:{$ne:"red"}})
{ "_id" : ObjectId("4fe0ebac8ee0adc8c442fa02"), "colors" : [ "blue", "black" ] }
>

Looks like $ne at least is handled specially. I thought this would match both documents, since both of them have some value that is $ne to "red".

Daniel Harman

unread,
Jun 20, 2012, 2:24:49 PM6/20/12
to mongodb...@googlegroups.com
Hi all,

Is there any new info on mongo as a service on the new azure? I saw some references to 10gen having a new service offering in the microsoft announcements a couple of weeks ago, but haven't found any new info on my searches?

I like the look of new azure, but want to avoid self hosting. I know app harbor all geared up with mongohq and the other competitor already integrated as services.

Dan

sridhar

unread,
Jun 20, 2012, 7:53:29 PM6/20/12
to mongodb...@googlegroups.com
Please create a new thread when the question is on a different subject

What we announced as part of the "Meet Windows Azure" event was the MongoDB Installer for Windows Azure Virtual Machines (http://www.mongodb.org/display/DOCS/MongoDB+Installer+for+Windows+Azure). We are working to make the process for installing/using/managing MongoDB on Windows Azure easier and more automated and the installer is the first in the tool chest.

Daniel Harman

unread,
Jun 21, 2012, 9:27:13 AM6/21/12
to mongodb...@googlegroups.com
How does threading work on this forum? I changed subject and removed any visible content from the previous message as was most convenient on an iPad.  Very surprised it has threaded this together.

I'm still on iPad so no idea how to dethread given your reply also been threaded in.

Dan

Sent from my iPad

craiggwilson

unread,
Jun 21, 2012, 9:32:54 AM6/21/12
to mongodb...@googlegroups.com
Instead of posting a reply (removing content and changing subject), you would just create a new topic.

Robert Stam

unread,
Jun 21, 2012, 9:33:18 AM6/21/12
to mongodb...@googlegroups.com
To start a new thread just send a brand new email to mongodb...@googlegroups.com instead of replying to an existing one. Or create a new post using the Google Groups web interface.

Hitting reply and clearing the subject line and content area isn't enough. There must be something preserved in the email headers that keeps it tied to the original thread.

craiggwilson

unread,
Jun 21, 2012, 9:34:02 AM6/21/12
to mongodb...@googlegroups.com
@TM: We have pushed a new commit to master that should contain the code you need to do handle your situation.  If you have some time, would you mind pulling the latest and verifying it fixes your issue.  Thanks...

TM

unread,
Jul 10, 2012, 11:13:30 AM7/10/12
to mongodb...@googlegroups.com
Sorry for the late response. I just pulled the last version and will run some tests, but the syntax looks clean now.

In this context a have another question:

How would I do this with the QueryBuilder?

Query.EQ("Access._id", 4711)

I could use ElemMatch, but this doesn't feel right here. My object looks something like this:

{ "_id" : 5,
"Access":[{"_id":4711}, {"_id": 4712}]
}

Greets

craiggwilson

unread,
Jul 10, 2012, 11:59:23 AM7/10/12
to mongodb...@googlegroups.com
Yes, you'll need an elemMatch for this.  It would be awkward to do this in the a typed api as it would require getting a single element out of a list (Query<MyClass>.EQ(mc => mc.Access.Single().Id, 4712).  Anyways, we don't currently support this via the typed api. If elemMatch isn't what you want here, you'll need to use the untyped query api.
Reply all
Reply to author
Forward
0 new messages