i want to search in the whole users model values, if admin searches for example a number or a string if the there is a documnet with that value (or that value includes the admin input) in the users model values, mongoose should return all found documents.
how can i implement such thing?
something like this :
const adminInput = req.query.searchText
const foundDocument = await users.find({ '*': includes(adminInput) }) // very simple xD
res.send(foundDocument)
As #NeNad points out, this question has been asked and answered many times. Another example is this question.
I'm just chiming in about two things in particular about the phrasing of this question that caught my attention. The first is:
if admin searches for example a number or a string if the there is a documnet with that value (or that value includes the admin input)
This type of searching can only be applied to string values. There is no concept of 3 being "in" the value of 537. There is, however, the concept of the character "3" being inside of the string "537".
The second thing somewhat overlaps with that and the includes(adminInput) from the code. The direct $text searching functionality provided in the database does not handle partial matches. You would likely need to look into alternatives such as Atlas Search or coupling Elasticsearch beside your MongoDB cluster to get the full functionality you are describing.
Related
I've finally started to understand a lot of info regarding FireStore, but I'm wondering if I can get some assistance.
If I had a setup similar to or like this:
races
Android
name: Android
size: medium
stats <---- this is the map
str: 10
sex: 12.... (more values)
How would I parse this? I am looking to make specific TextViews apply values found in the database so that I can simply update the database and my app will populate those values so that hard coding and code updating won't be nearly as troublesome in the future.
I currently use something like this:
val androidRef = db.collection("races").document("Android")
androidRef.get().addOnSuccessListener { document ->
if (document != null) {
oneOfTheTextViews.text = document.getString("str")
} else {
}
The issue is currently I can only seem to access from collection (races) / document (android) / then a single field (I have "str" set as a single field, not part of a map or array)
What would the best practice be to do this? Should I not nest them at all? And if I can reference said nesting/mapping/array, what functions need to be called? (To be clear, I am not asking only whether or not it is possible - the reference guides and documents allude to such - but what property/class/method/etc needs to be called in order to access only one of those values or point to one of those values?).
Second question: Is there a way to get a list of document names? If I have several races, and simply want to make a spinner or recycler view based on document names as part of a collection, can I read that to the app?
What would the best practice be to do this?
If you want to get the value of your str property which is nested within your stats map, please change the following line of code:
oneOfTheTextViews.text = document.getString("str")
to
oneOfTheTextViews.text = document.getString("stats.str")
If your str property is a number and not a String, then instead of the above line of code please use this one:
oneOfTheTextViews.text = document.getLong("stats.str")
Should I not nest them at all?
No, you can nest as many properties as you want within a Map.
Is there a way to get a list of document names?
Yes, simply iterate the collection and get the document ids using getId() function.
I have a checkbox group and I am trying to get the values selected via SSJS, but so far I have not been successful. I've tried several syntaxes, such as:
document1.getItemValueArray ("nameField")
and
getComponent ("nameField") getSelectedValues ();
Does anyone know a way to get the selected values from a checkbox group?
document1.getFirstItem("nameField").getValues() may be what you want. If it's one value, it will be a string, not a Vector, which may be a problem with getItemValueArray().
With ODA (OpenNTF Domino API), we extended the getItemValue() method to take a second parameter and cast the result to that kind of object. That has a big benefit for this kind of scenario, allowing getItemValue("nameField", ArrayList.class) to always return an ArrayList even for a single value, plus ArrayList is a much better and more modern Java (so relevant also for SSJS) construct than a Vector.
The question
In short, my question is: when an array in a document is changed, will the users receive the new array, or just the changes?
If that question is unclear, I've described my problem below.
The problem
I have a collection whose documents contain an array field two users will push values to. A document in this collection kind of looks like this:
var document = {
userId1: "...user id...", // The id of the first of the two users.
userId2: "...user id...", // The id of the second of the two users.
data: [] // The field the two users will push values to.
}
data will from the beginning be empty, and the users will then take turns pushing values to it.
When one of the user pushes some value to data, the server will send the changes to the second user. Will the second user receive the entire data-array, or just the changes (the pushed value)? I'm a little bit worried that the second user will receive the entire data-array, even though it's just a single value that's been pushed to it, and if data contains many values, I fear this will become a bottleneck.
Is this the case? If it is, using another collection for storing the values will solve it, right? Something like this:
var document = {
id: "...unique id...",
userId1: "...user id...", // The id of the first of the two users.
userId2: "...user id..." // The id of the second of the two users.
}
var documentData = {
idReference: "...the unique id in the document above...",
value: "...a value..."
}
Instead of pushing the values into an array in document, insert them into a collection containing documentData. This (I know) won't have the downside I fear the first solution has (but I rather use the first solution if it doesn't have the downside).
As per https://github.com/meteor/meteor/blob/master/packages/livedata/DDP.md
changed (server -> client):
collection: string (collection name)
id: string (document ID)
fields: optional object with EJSON values
cleared: optional array of strings (field names to delete)
Users will receive the new array. To only send "diffs," use a collection of {userId: userId, value: value} documents.
I inspected what was sent as commented by user728291, and it seems like the entire array-field is sent, and not just the pushed value. I don't know if this always is the case (I just tested with an array containing few and small values; if it contains many or big values Meteor maybe try to do some optimization I couldn't see in my tiny test), but I'll go with the solution using another collection instead of the array-field.
I have a question according to the WCF Data Services 5.0.1 Any/All-Features. I want to use it in a Silverlight 5 Application and I want to query against an Entity called "Employee" (with a unique EmpNo=personalNr) and check if it already exists (therefore, I check if there is an Employee with the same personalNrfor validation purposes)..
In older versions it was not possible to do this on the Client. I had to call a custom Service Operation on the Server which returned a boolean value.
Is there a way to do this on the Client likes this (and get a boolean value as a result):
bool result = this.Context.Employees.Any(e => e.PersonalNr.Equals(personalNr, StringComparison.OrdinalIgnoreCase));
Thanks in advance!
Steve
The any/all feature is only usable inside the filter expression and it is used to query based on related entities or collection properties. If you want to check just for existence of an employee without any relationship, you can do that without any/all. The idea is to simply filter all employees on the given condition and see if you get at least 1 result back.
Now since you're doing this in Silverlight, the operation must be asynchronous, so a simple statement like above will not work. You could do something like:
var query = (DataServiceQuery<Employee>)this.Context.Employees.Where(e => e.PersonalNr.ToLower() == personalNr.ToLower()).Take(1);
query.BeginExecute((ar) =>
{
var results = query.EndExecute(ar);
// The usage of Any here is simply because it's the easiest way to do this
// and it is not used over OData/WCF DS, this is simply checking if the results returned
// from the service contain at least one result.
bool employeeExists = results.Any();
}, null);
Few notes about the code above:
The WCF Data Services doesn't support the Equals method with comparison options and the OData protocol doesn't support case insensitive string comparison either. So to workaround that, simply convert all values to lower case before comparing.
The Take(1) is used to only ask for the first value which matches the condition. Since we're only gonna use the existence of the result anyway, we don't need to ask the service for all the results (small optimization).
I need to filter entities based on one of their ListProperties having a certain element present. So kind of like:
entities.filter('listProp IN ',element) except where listProp and element are reversed if you see what I mean.
Anyone know how to filter like this?
If I understand you correctly, you want to find all entities which have that particular element present. You should be able to use: entities.filter('listProp =', element)
Look at: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#ListProperty
It says, "list_property = value tests if the value appears anywhere in the list".
Ok so it turns out the IN equality clause takes care of this case for lists automatically.
As in it does a for ... each on the list of elements to be searched for and if any one of them is present in the ListProperty for each entity it will return that entity.