How to lowercase field data in MongoDB find() - javascript

In my database collection I have two Objects.
[
{"_id" : 1, name: "notLowercased"},
{"_id" : 2, name: "lowercased"},
]
I'm using find and $regex to find name that includes some string.
data = await CatalogModel.find({name: {$regex : searcher.toString().toLowerCase()}})
For example my input string is "lowercased".
In result I'm getting an array
[
{"_id" : 2, name: "lowercased"},
]
But I want to get in result this:
[
{"_id" : 1, name: "notLowercased"},
{"_id" : 2, name: "lowercased"},
]
I'm understand that it's happening becase name "notLowercased" not lowercased.
How to lowercase name fields in this request?

You can add $options parameter like this: $options: "i".
As explained into docs:
i: Case insensitivity to match upper and lower cases. For an example, see Perform Case-Insensitive Regular Expression Match.
Even you can avoid toLowerCase()
data = await CatalogModel.find({name: {$regex : searcher.toString(), "$options": "i" }})
Example here and without toLowerCase() here

Related

remove extra spaces in string in javascript

I have a text and after deleting special characters (!##$%^&*()-=+`";:'><.?/) and show just letters and numbers (and float numbers like 23.4 ) it returns some extra space
const input : 'this is a signal , entry : 24.30 and side is short';
const text = input.replace(/\.(?!\d)|[^\w.]/g, " ").toUpperCase();
console.log(text.split(" "))
the output :
[
'THIS', 'IS', 'A',
'SIGNAL', '', '',
'', 'ENTRY', '',
'', '24.30', 'AND',
'SIDE', 'IS', 'SHORT'
]
but I want to be this :
[
'THIS', 'IS', 'A',
'SIGNAL', 'ENTRY', '24.30',
'AND', 'SIDE', 'IS',
'SHORT'
]
And when I replace spaces and enters with empty string , returns this :
[ 'THISISASIGNALENTRY24.30ANDSIDEISSHORT' ]
what is the problem of my code?
Instead of replacing, consider matching all the sorts of characters you want to produce the array of words. It looks like you want something like:
const input = 'this is a signal , entry : 24.30 and side is short';
const matches = input.toUpperCase().match(/[\w.]+/g);
console.log(matches);
The second parameter in the replace method needs to be an empty string and not a space as you have it in your code.
Just do:
...
const text = input.replace(/\.(?!\d)|[^\w.]/g, "").toUpperCase();
...

MongoDB, mongoose, update object inside array

I have the following MongoDB model:
const Relation = mongoose.model('Relation',{
name :{
type: String,
},
port:{
type: Number,
},
services: { type : Array , "default" : [] }
});
Each port is a unique number for each document.
A collection could have the following values:
{
"port":"116", //unique number
"name":"xzy",
services: [
{"id":'1', "trust":"good"},
{"id":'2', "trust":"bad"},
]
}
How can for example make the "trust" value "bad" for the object with the "id"= 1 ??
I assume I should first find the collection that matchs the port number "116" and then find the object inside the Services array that has the "id" of 1.
How can I do that in mongoose?
You can use $ positional operator to update value inside an array
Relation.findOneAndUpdate(
{ "port": "116", "services.id": "1" },
{ "$set": { "services.$.trust": "bad" }}
)

Remove Attribute names from Json string

Hi lets say I have a JSON string which represent a record in a grid containing 3 columns Id, Name, Status. I'm currently writing some JavaScript logic where you can filter the rows of data by typing some text in the text box. The filter will be applied to data in all columns. So if I type "James" Record 1 below will be returned an if I type None Record 1 and 2 will be returned. The problem is if I type Id, Name, or Status which is not the data but the attribute names, all records are always returned.
Record 1
{
Id: 1,
Name: "James",
Status: "None"
}
Record 2
{
Id: 2,
Name: "Paul",
Status: "None"
}
How can I modify a JSON string so that
{ Id: 2, Name: "Paul", Status: "None"}
will become
{ 2, "Paul", "None"}
Your question is a bit unclear (and I'm afraid Matthias' edit made that matter worse).
{ Id: 1, Name: "James", Status: "None" } is not a valid JSON string, but it is a valid Javascript object. JSON strings need to have their values within quotes.
If you are indeed dealing with a JSON string, with quoted properties, and you simply want the output you've requested, you could do something like this:
var person = '{ "Id": 1, "Name": "James", "Status": "None" }';
person = person.replace(/\s*"[^"]+"\s*:/g,"");
// > person = '{ 1, "James", "None" }'
If you are dealing with a Javascript object, a simple way of getting the values without the property names would be to do something like this:
var person = { Id: 1, Name: "James", Status: "None" };
person = Object.keys(person).map(function(k) { return person[k] }).join(',');
// > person = '1,James,None'
Both options will give you a string that you could search for just the values you're interested in. In the latter scenario, you'd need to add some formatting to turn the outcome into exactly what you have requested, but then given the question I'm assuming presentation is not a big deal.
However, if at all possible, I think your code would much more closely match your intentions if you instead modified the search algorithm to inspect values and not entire objects. You haven't shown us any of the code doing the searching, though, so I can't really add suggestions for that at this point.

Array of values

For example I have n doc in MongoDB collection
Films = new Mongo.Collection('films');
Film.insert({name: 'name n', actor: 'John'}); // *n
And I want to show array with only name values
var names = ['name 1', 'name 2',..,'name n'];
Any idea how to do it ?
And guys , ols write in comments correct title value of my question, to help other guys to find it, thx :)
You didn't provided any criteria for grouping name as an array.
You can use following query to get all names:
db.collection.distinct("name")
OR you can use MongoDB's aggregation to get all name by grouping them with some condition, if required. The query will be like following:
db.collection.aggregate({
$group: {
_id: null, //add condition if require
name: {
$push: "$name"
}
}
}, {
$project: {
name: 1,
_id: 0
}
})
If you want only distinct name then replace $push with $addToSet.

Why is it not possible to find documents with '$all' modifier?

I have the following documents:
{
_id: 1
title: "oneItem"
},
{
_id: 2,
title: "twoItem"
}
When I try to find these documents by using the following command:
db.collection.documents.find({_id: {$in: [1, 2]}});
I get these two documents but when I try to find these documents by using the following query:
db.collection.documents.find({_id: {$all: [1, 2]}});
I get nothing. Can you explain what's the problem? Basically I need to find all documents with _id 1 and 2 and if none exist then fail.
The reasoning is actually quite simple in that $in and $all have two completely different functions. The documentation links are there, but to explain:
Consider these documents:
{
_id: 1,
items: [ "a", "b" ]
},
{
_id: 2,
items: [ "a", "b", "c" ]
}
$in - provides a list of arguments that can possibly match the value in the field being tested. This would match as follows:
db.collection.find({ items: {$in: [ "a", "b", "c" ] }})
{
_id: 1,
items: [ "a", "b" ]
},
{
_id: 2,
items: [ "a", "b", "c" ]
}
$all - provides a list where the field being matched is expected to be an array and all of the listed elements are present in that array. E.g
db.collection.find({ items: {$all: [ "a", "b", "c" ] }})
{
_id: 2,
items: [ "a", "b", "c" ]
}
Hence why your query does not return a result, the field is not an array and does not contain both elements.
The MongoDB operator reference is something you should really read through.
As your your statement, ["I need to find all documents with _id 1 and 2 and if someone from them does not exists then fail."], matching various criteria is easy as you see in the usage of $in. Your problem is you want a whole set to match or otherwise return nothing ("fail"). This I have already explained to you an some length in a previous question, but to re-iterate:
db.collection.aggregate([
// Match on what you want to find
{$match: { list: {$in: [1,2]} }},
// Add the found results to a distinct *set*
{$group: { _id: null, list: {$addToSet: "$_id"} }},
// Only return when *all* the expected items are in the *set*
{$match: { list: {$all: [1,2]} }}
])
So after that manipulation, this will only return a result if all of the items where found. That is how we use $all to match in this way.

Categories

Resources