What is better way to query mongodb array field? - arrays

I am new to mongodb. So I don't know what is the better way to query array field. I have a schema where in our collection of courses we have a program field. In start it was Reference to program collection and contain program id. But Now we have change the schema from Reference to array of Reference, so we can allow course to be a part of multiple program. In our codebase all the query are written like this:
course.find({program});
Do I have to change this query to cater this schema change like this:
course.find({program: {$in: program}});
I have tested in mongodb compass and this query
course.find({program});
Work on array field.
So let me know what could be consequence if i don't use $in operator while search the array field.

Related

Matching documents that contain a string array where at least one of the array elements is not an empty string

I have documents that contain simple arrays of strings, and I can't seem to set up a filter that is capable of bringing back all documents where a given array field has at least one element string that is not "". This is on a collection with 6500 documents, where 3700 should meet the above criteria (I checked by pulling all records and performing the filter client-side).
I am mainly using the driver in .NET, but I've also tinkered with the filter in Compass. Using the driver I've tried Ne, Not(Eq), AnyNe, Not(AnyEq), Nin[""], Not(In[""]). I would like to use ElemMatch, but it seems like that is geared towards arrays of documents not arrays of strings, since you have to specify a field name, which doesn't exist in this case. I've also tried setting up a .Where filter that looped through to find any non-empty string in the array, but it threw an exception at run-time (I'm coding in VB).
Builders(Of BsonDocument).Filter.AnyNe(Of String)("field", String.Empty)
I would expect that the above filter, where "field" is a reference to an array of strings, would bring back 3700 documents but I get 0.
I would imagine that I'm clearly the one missing something here, as this does not seem like it should be a difficult query/filter to construct. Any help would be greatly appreciated.
For anyone as confused as I am:
I mentioned earlier that ElemMatch seems to be geared for arrays of documents, but apparently if you forgo using the Builder classes and type out the query manually you can actually use ElemMatch within .NET to query a simple String Array field where at least one entry is not empty string "".
Correct/Working example: {"[array_field_name]": {$elemMatch: {$ne: ""}}}
If anyone can tell me how to create that example using the Builder classes, that'd be awesome.

Cannot create index for new Firestore query feature "array-contains"

I am using the new Firestore query feature
firebase.firestore().collection("articles").where("tags", "array-contains", tag)
to query all the article with specific tags.
The query works fine but the result is not sorted.
I understand how database indexes work and use them with other string fields before but not yet with array type.
When I do the query
firebase.firestore().collection("articles").orderBy("publishTime", "desc").where("tags", "array-contains", tag)
It prompts a link to create index. But that link does not work.
Is this because the new feature is still under development or is there any special step for "array-contains" query type?
This is now resolved by create the index field under ARRAY type

Mongo DB Query to check if document array field element present in more than one document

I have been searching through the MongoDB query syntax with various combinations of terms to see if I can find the right syntax for the type of query I want to create.
We have a collection containing documents with an array field. This array field contains ids of items associated with the document.
I want to be able to check if an item has been associated more than once. If it has then more than one document will have the id element present in its array field.
I don't know in advance the id(s) to check for as I don't know which items are associated more than once. I am trying to detect this. It would be comparatively straightforward to query for all documents with a specific value in their array field.
What I need is some query that can return all the documents where one of the elements of its array field is also present in the array field of a different document.
I don't know how to do this. In SQL it might have been possible with subqueries. In Mongo Query Language I don't know how to do this or even if it can be done.
You can use $lookup to self join the rows and output the document when there is a match and $project with exclusion to drop the joined field in 3.6 mongo version.
$push with [] array non equality match to output document where there is matching document.
db.col.aggregate([
{"$unwind":"$array"},
{"$lookup":{
"from":col,
"localField":"array",
"foreignField":"array",
"as":"jarray"
}},
{"$group":{
"_id":"$_id",
"fieldOne":{"$first":"$fieldOne"},
... other fields
"jarray":{"$push":"$jarray"}
}},
{"$match":{"jarray":{"$ne":[]}}},
{"$project":{"jarray":0}}
])

Index map values

I have data in which field have following java data types.
What would be the best way to index such kind of data.
Thanks,
field_a map<string,string>
field b map<string,array<string>>
How to define schema.xml for it
Currently Solr doesn't support map type field type. So, you can not query on some particular key inside the map and retrieve its value. I don't know whether it'll be helpful you or not, but I can suggest you a way to keep this in Solr.
You can store the map in a field as a json formatted string. Say, document1 has map1 in field_a and document2 has map2 in field_a. Now, you keep some distinct data related to each map to their corresponding documents. When you want to query, query on those fields in stead of the maps. Then in the search result, when you retrieve the json formatted string, parse it in your application and get the values.
Hope this will help.

MongoDB - get all documents with a property that is saved in a list in a document of another collection

I have documents in a collection that have an array of properties (1-400 different numeric values).
Now i want to get all documents of another collection that have one of these properties (these documents only have one property).
How can i do that, preferably in one call?
As MongoDB is no relational DBMS this isn't possible to achieve with only one call.
What you need to do is to first retrieve your document your want to use for your search. Upon you retrieved it, you're using that array stored in the document todo a $in query on the field for the other collection. So for the mongo shell this could be something like this:
var ar = db.coll1.findOne().numArray
db.coll2.find({b: { $in : ar }})

Resources