I have products with a price, I want to find products whose price will be less than 10 but more than 5. So that SQL query looks like this:
SELECT * FROM products WHERE products.price < 10 AND products.price > 5
Is it possible to do this without using Query Builder?
I don't find the And operator in the documentation
The better way is to use Between Operator in your case. Between(1, 10) 1<x<10
You just need to use where operator.
userRepository.find({ where: { firstName: "Timber", lastName: "Saw" } });
This code executes this query:
SELECT * FROM "user" WHERE "firstName" = 'Timber' AND "lastName" = 'Saw'
And if you want to use OR, you use an array of conditions:
userRepository.find({
where: [
{ firstName: "Timber", lastName: "Saw" },
{ firstName: "Stan", lastName: "Lee" },
],
});
This code will executes this query:
SELECT * FROM "user" WHERE ("firstName" = 'Timber' AND "lastName" = 'Saw') OR ("firstName" = 'Stan' AND "lastName" = 'Lee')
More infos in the doc: https://github.com/typeorm/typeorm/blob/master/docs/find-options.md
These docs clarify how to use find like SQL
https://orkhan.gitbook.io/typeorm/docs/find-options
Note that for conditional where and other statements, you need QueryBuilder.
Related
I want to exclude for example email and address using populate() function from mongodb, just get the name from it:
Example:
const results = await Seller.aggregate(aggregatePipeline).exec();
const sellers = await Seller.populate(results, { path: "user" });
When populating the user instead of having:
...
user: {
email: "hgjh#gmail.com",
address:{},
name: "name"
}
I want to only have (exclude certain data from the path):
...
user: {
name: "name"
}
You can do either,
const sellers = await Seller.populate(results, { path: "user", select: '-
email -address' });
or
const sellers = await Seller.populate(results, { path: "user", select:
'name' });
As i understand mongoose documentation https://mongoosejs.com/docs/populate.html, populate as $lookup is use to resolve a relation with other collection.
MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections.
In your case, you don't need to resolve a field with an other collection. You already have the final data you target . You could use $project at the end of your pipeline aggregation to keep only name field, like :
{ $project: { name:1 } }
Let me know if i helped you.
Edit :
I read too fast, if you have this data res after the populate and not after the aggreg, you may select your final field, like is said
here https://stackoverflow.com/a/72481338/16205278
user: {
email: "hgjh#gmail.com",
address:{},
name: "name"
}
I've a collection of countries with country calling code in the country object. How can I find a country using calling code with a mobile number?
const countries = [
{
name: 'UAE',
callingCode: 971
},
{
name: 'USA',
callingCode: 1
},
{
name: 'UK',
callingCode: 44
}
];
const number = '971524500000'; // Input
How can I find country for the given mobile using regex in mongoose javascript;
[https://en.wikipedia.org/wiki/List_of_country_calling_codes][1]
Take a look at the link above on country calling codes, and specifically see the section "Tree List".
One solution would be to implement a schema in Mongo based on this tree in order to decode the country codes.
So, a table could be created to store Mongo documents containing a field "1x" "2x" "21x" etc (the Y axis in the Tree List table).
Each of these documents could contain an array of sub-documents from x=0 to x=9 (the x axis in the Tree List table). The sub-document can contain the country name/code you are looking for. You can use a direct index into the array in the Mongo document for an efficient lookup.
I think you'll find this to be a pretty efficient implementation and should cover all the bases.
If you can restructure your array to an object this would be the fastest
const countries =
{
971: 'UAE',
1: 'USA',
44: 'UK',
}
;
var code = 44;
console.log(countries[code]);
const countries = [
{
name: 'UAE',
callingCode: 971
},
{
name: 'USA',
callingCode: 1
},
{
name: 'UK',
callingCode: 44
}
];
var myFound =countries.filter(myFunc.bind(this,44));
function myFunc(code,element) {
if(element.callingCode == code){
return element;
}
}
console.log(myFound);
On MongoDB v 4.2 - you can use $let & $regexFind to do this :
db.collection.aggregate([
{
$match: {
$expr: {
$eq: [
{
$let: {
vars: {
value: {
$regexFind: {
input: "971524500000", // pass in as string
regex: { $toString: "$callingCode" }
}
}
},
in: "$$value.idx",
}
},
0
]
}
}
}
]);
Test : MongoDB-Playground
Explanation :
General Use-case :
In general regex searches - Will have an input which will be sub-string of actual string, Ex.:-
Actual string in DB : 'We can do it in mongo'
Input : mongo (/mongo/ - will work)
Your Use-case :
From above case as mongo exists in actual string/value of db field then you can get that document, But your case is different :
Actual string in DB : mongo
Input : 'We can do it in mongo'
This doesn't work that way, So using normal /We can do it in mongo/ isn't going help you here (Also doing few tricks with regex). So we need to make a trick to $regexFind operator. Unlike mongo documentation we need take 971524500000 into input field & regex as string value of callingCode field which is vice-versa to what's given in documentation.
So once we do that, We would get something like below :
{
"match" : "971", /** value of callingCode field */
"idx" : 0, /** Index of `971` in '971524500000' */
"captures" : []
},{
"match" : "1",
"idx" : 2,
"captures" : []
},
null /** 3rd doc no match */
As country code has to be at first of given number we need docs where "idx" : 0 - So we're using $let to get index of returned object & checking against 0 & eventually getting respective docs using $match.
Note :
There is something you need to look into, Just in case if you've docs like below :
{
"_id": ObjectId("5e8f67091aa1cc3d2158ade1"),
"name": "USA",
"callingCode": 1.0
},
{
"_id": ObjectId("5e8f67091aa1cc3d2158ade3"),
"name": "somecountry",
"callingCode": 197.0
}
& input is 1971524500000, then this query will bring both docs in result. This will be the case you need to check on. Anyhow I would suggest to try this query, rather than getting all documents for collection to the code & extract required o/p this might be better to do.
I got the following data model: https://imgur.com/a/AwwpW9F
Basically, A User can belong to many Projects, and a Project can have many Users and I'm tying that together through a join table called UserProjects
With a raw SQL query I can go
SELECT "user".email, project.name FROM "user"
JOIN userprojects ON userprojects.user_id = "user".id
JOIN project ON project.id = userprojects.project_id
Which gives me
email(On User table) name(On Project table)
first#email.com Project X
first#email.com Project Y
second#email Project Y
How would I structure this query with Objection ORM? Perhaps I can just do a raw query straight? Something like
User.query().raw(SELECT "user".email, project.name FROM "user"
JOIN userprojects ON userprojects.user_id = "user".id
JOIN project ON project.id = userprojects.project_id)
?
instead of doing all the stuff by yourself, Objection.js can do it for you. You can just declare a ManyToManyRelation.
static relationMappings = {
projects: {
relation: Model.ManyToManyRelation,
modelClass: Project, // imported Objection class of "Project"
join: {
from: 'user.id',
through: {
from: 'userprojects.user_id',
to: 'userprojects.project_id'
},
to: 'project.id'
}
}
}
Then you can just get the projects of a User using eager loading:
User.query().eager('projects').findById(userId)
And you will get something like:
User {
id: 3,
firstname: 'firstname',
lastname: 'lastname',
email: 'email',
projects: [
{id: 1,
name: 'name1'},
{id: 2,
name: 'name2'},
]
}
2020 Update:
Since version 2 of Objection.js, eager method has been renamed as withGraphFetched:
User.query().withGraphFetched('projects').findById(userId)
Nvm, found a solution
/close
JK,
Here's what worked for me if anyone else would run into the same issue:
return User.query().where("user_id", parent.id)
.join('userprojects', 'user.id', '=', 'userprojects.user_id')
.join('project', 'project.id', '=', 'userprojects.project_id')
.select('user.id', 'userprojects.project_id', 'project.name')
I just started exploring mongoDB. During that time I managed to create a simple collection with the following data,
db.test.insert({name : "joe", age : 20});
db.test.insert({name: "james", age : 25});
Now I have queried it by using,
db.test.find({name : {$not : {$eq : "joe", $eq: "james"}}})
It yielded me the following result,
{name : "joe", age : 20}
I am confused here, Is it not an and operation like below,
SELECT * FROM test where not name = "joe" and not name = "james"
Doubt1: If yes then how the above mentioned mongo query can be interpreted as a normal mysql query? [Note : I am coming from a mysql background.] Please explain.
Doubt2: Also explain about the following mongo query,
db.test.find({name : "joe", age : 20});
Is the above query equals to select * from test where name='joe' and age=20 ? If not then how it would be interpreted as a normal mysql query?
When you are creating an object with {$eq : "joe", $eq: "james"} you are using the same object key twice. So james will overwrite joe because both use the same object key $eq.
To do your query, you can try something like NOT (name = "joe" OR name = "james"). So you add the OR to your mongo query:
db.test.find({name : {$nor: [{$eq: "joe"}, $eq: "james"]}})
Reference
I guess Christoph cleared your doubts.
For SQL to Mongo Query reference visit this link
I need to find the object whose name is "abc" from category collection in mongodb. Here is the my code
[{
_id: 728e38e7,
name: 'abc,def,ghi,klm,nmo',
place: "mys"
},
{
_id: 2788,
name: 'djhd',
place: "bang"
}]
try that
var query = { name: new RegExp('^' + abc) };
collection.find(query).toArray(function(err, items))
Using the mongo shell you can use find().
db.collection.find({"name" : /abc/}).
From this post : How to query MongoDB with "like"?
Below is the simplest method of using regex to find element based on name in mongodb :
db.collection.find({name:{$regex:"ABC",$options:"$i"}})
Here $i is used to make the search case insensitive.