search in a map field - mongodb

I'm using morphia. As u know for a simple search I can use this:
q.field("fieldname").containsIgnoreCase(texttosearch);
But my field type is map. So I must change it like this(use dot):
q.field("mapname.fieldname").containsIgnoreCase(texttosearch);
But again I want to search in all fields. I can simply do this by repeating for all fields. The problem is my field count is not static.
How can I solve this?

You should store it not as a map, but as an array/list of key/value pairs. Then you can search the key field, and index it. This is not something morphia does for you at the moment but something which could be an alternate storage format for Map. It would be a very different format.
Take a look at this discussion for more background

Related

Flutter project Firestore data sorting

I have some data on my project firestore. It seems like this:
I am trying to sort this data by SKU. As you can see it is in a list. How can I give its field path to orderBy() method?
There is no way to sort the document you've shown on a SKU value, since that value exists in an array field and there could be multiple values.
If you want to sort on a specific SKU value, add a top-level field (e.g. skuForOrdering) to the document and sort on that with orderBy('skuForOrdering').

Firestore check if array in specific document contains a value

I would like to check if the array "devices" which is in the document "SOjTxEjMwb71isiiPnR8" contains the value "AE0EBAFA-1560-404B-A3DA". I just want to know this specific array in the specific document. Because it can be that the same value in the same array occurs in another document. How do I have to do this in Swift? Thanks a lot for your help.
You're looking for the array-contains operations, which in Swift looks like this:
devicesRefRef
.whereField("devices", arrayContains: "AE0EBAFA-1560-404B-A3DA")
You can then get the results once or listen for realtime updates. You'll note that all links I provide are to the relevant sections of the documentation, so I recommend spending some time there.

Transform text value to lower case when indexing

Any idea how can I transform text value to lower case when indexing in firestore function. I have tried this
const newRef = db.collection("user").where("profile.companyName".toLowerCase(), ">=", data.value.toLowerCase())
but it give me an error at this part
"profile.companyName".toLowerCase()
Is it even possible to do it? I really don't want to save all names to lowercase to be able to properly indexing through my names and then capitalise them
Firestore doesn't have an API or mechanism to do this for you. You will have to store the lowercased version of the string in a field, and use that field to make your query.

MongoDB: Update all fields

Is there a way I can update every field in a MongoDB document without just listing all the fields, which might change later? Something like this?
db.update(
{foo:"bar"},
{$unset: {{}:""}}
)
Consider the cogency of an operation like this. How are you updating all of the fields meaningfully without considering their specific types or values? It looks like you are trying to unset the fields or set them to a default, null-like value. What does it mean to unset every field in a document? Isn't it really just removing the document? Are you excluding _id and other immutable fields (shard key!) somehow? What is the default value for the possible field types? What is the default value for a field that can be an integer or an array of integers in a data model? It's also dangerous to try to do something like this, especially with flexible schemas. Needing to do the above operation indicates a good chance that you need to rethink your data model.

MongoDB - forcing stored value to uppercase and searching

in SQL world I could do something to the effect of:
SELECT name FROM table WHERE UPPER(name) = UPPER('Smith');
and this would match a search for "Smith", "SMITH", "SmiTH", etc... because it forces the query and the value to be the same case.
However, MongoDB doesn't seem to have this capability without using a RegEx, which won't use indexes and would be slow for a large amount of data.
Is there a way to convert a stored value to a particular case before doing a search against it in MongoDB?
I've come across the $toUpper aggregate, but I can't figure out how that would be used in this particular case.
If there's not way to convert stored values before searching, is it possible to have MongoDB convert a value when it's created in Mongo? So when I add a document to the collection it would force the "name" attribute to a particular case? Something like a callback in the Rails world.
It looks like there's the ability to create stored JS for MongoDB as well, similar to a Stored Procedure. Would that be a feasible solution as well?
Mostly looking for a push in the right direction; I can figure out the particular code once I know what I'm looking for, but so far I'm not even sure if my desired functionality is doable.
You have to normalize your data before storing them. There is no support for performing normalization as part of a query at runtime.
The simplest thing to do is probably to save both a case-normalized (i.e. all-uppercase) and display version of the field you want to search by. Suppose you are storing users and want to do a case-insensitive search on last name. You might store:
{
_id: ObjectId(...),
first_name: "Dan",
last_name: "Crosta",
last_name_upper: "CROSTA"
}
You can then create an index on last_name_upper, and query like:
> db.users.find({last_name_upper: "CROSTA"})