mongodb array matching

2019-05-18 21:39发布

问题:

How do I accomplish the following?

db.test.save( {a: [1,2,3]} );

db.test.find( {a: [1,2,3,4]} );  //must match because it contains all required values [1, 2, 3]
db.test.find( {a: [1,2]} );  //must NOT match because the required value 3 is missing

I know about $in and $all but they work differently.

回答1:

Interesting..The problem is..the $in and the $or operators get applied on the elements of the array that you are comparing against each document in the collection, not on the elements of the arrays in the documents..To summarize your question: You want it to be a match, if any of the documents in the collection happens to be a subset of the passed array. I can't think of a way to do this unless you swap your input and output. What I mean is..Let's take your first input:

db.test.find( {a: [1,2,3,4]} );

Consider putting this in a temporary collection say,temp as:

db.temp.save( {a: [1,2,3,4]} );

Now iterate over each document in test collection and 'find' it in temp, with the $all operator to ensure it is completely contained, i.e., do something like this:

foreach(doc in test)
{ db.temp.find( { a: { $all: doc.a } } ); }


This is definitely a workaround! I am not sure if I am missing any other operator that can do this job.



回答2:

There is currently no operator that will find documents with a subset values. There is a proposed improvement to add a $subset operator. Please feel free to up vote the issue in JIRA: https://jira.mongodb.org/browse/SERVER-974

Other potential workarounds, which may not be suitable for your use case, may involve map reduce or the new aggregation framework (available in MongoDB version 2.2): http://docs.mongodb.org/manual/reference/aggregation/#_S_project