Is there a way with Spring Data MongoDB to execute string queries directly on DB without knowing the query that will be executed?
Example:
I want to execute this query:
db.users.find({ 'name': 'John' })
And imagine that this query is not always the same, and maybe it can change to:
db.clients.find({ 'company': 'XYZ' })
I want to know if it's possible to execute those queries directly on DB with the MongoTemplate, or possibly build a query from the string and then executing it.
Related
I've searched in the documentation of Prisma, but I couldn't find any information on how is possible to execute a PostgreSQL function like "to_char" using the prisma client.
In short, I would like to execute a query in which I order the data by month and day excluding the year, and all this done by the prisma client, without using raw query.
The question would be, if is there any possibility to implement this without using raw query?
While searching on the internet for solution, found this answer in raw query.
Select YourDate
From YourTable
Where YourConditions
order by to_char(YourDate,'MMDD')
I've managed to implement this by using $queryRaw method from prisma:
this.prismaService.$queryRaw(
Prisma.sql`
SELECT *
FROM User
WHERE to_char(birthdate, 'MMDD')>=to_char(NOW(),'MMDD')
ORDER BY to_char(birthdate,'MMDD')`
)
I was hoping to have some kind of function/operation available by Prisma to be able to make the query somehow in this way:
this.prismaService.user.findMany({
where: { birthdate: to_char(new Date(), 'MMDD') },
orderBy: { birthdate: { gte: { to_char(birthdate, 'MMDD') } } },
})
Appreciate any information!
I'm fairly new to mongodb but I was wondering if there's a way by which we can get 2 different results from same mongodb collection in one database call uisng mongo java driver with morphia.
I have a collection accounts and I'm fetching data based on a key accountId. I need below two results/outputs from this collection in one query.
count of all the documents where accountID is 'xyz'
ResultList of first N documents where accountID is 'xyz' AND resultSet is sorted by a timestamp field.
to resolve the second scenario I'm using:
..Query....limit(N).order("TimeField").field("TimeField").filter("accountID =", "xyz").asList();
This is working fine as per expectation but to get the total count (scenario 1) of all documents with accountId = 'xyz' needs another mongodb call, which I want to avoid.
MongoDB doesn't support such batching on queries, unfortunately. You'll have to execute two separate calls.
I just started with OrientJS and OrientDB and I try to insert multiple records at the same time.
If I use this code nothing happens, no error, but no insertion either, what do i do wrong?
testRecords.map(el =>
db
.insert()
.into('Profile')
.set({
email: el.email,
password: el.password,
verifyEmailToken: el.verifyEmailToken,
changePasswordToken: el.changePasswordToken,
})
.one());
If I use the OrientDB SQL and the db.query then it works fine with this code below (but with this it looks kinda strange how I have to setup the values, one example value from the value array looks like this example '("testemail#test.com", "123", "123456789", "123456789")'
const OSQL = `INSERT INTO ${className} (${properties}) VALUES ${testValues}`;
console.log(OSQL);
db.query(OSQL);
Can somebody guide me how to use properly the OrientJS API?
What I'm trying to do is take a user's query client side (via search function), send it to server via Meteor methods and if there is a search result, insert that query into another MongoDB instance? I need this in order to display search results from the user into client.
Something like this from server side:
UserSearch: function(query){
var init = Table.find({userId: this.userId}, {data: {Name: query}});
Search.insert({userId: this.userId, data: init});
},
Obviously, that doesn't work. But I'm looking for a way to allow to insert a query from one database into a new database.
So I think there are two pieces two your question here:
How do I connect to another MongoDB instance?
How do I insert data from one query result into a collection?
For #1, referencing this SO question, you can do:
var database = new MongoInternals.RemoteCollectionDriver("<mongo url>");
Search = new Mongo.Collection("search", { _driver: database });
For #2, you're simply missing the .fetch() as MasterAM pointed out:
UserSearch: function(query){
var init = Table.find({userId: this.userId, 'data.Name': query}).fetch();
Search.insert({userId: this.userId, data: init});
},
I also modified your Table query a bit as I don't think it's working in the current form. You want each field property in a query object to be in the same object. Note that the fetch will give you an array of Table objects, so you'll need to have your data field in your Search collection reflect that.
For MongoDB and Mongoose, does this cause 2 queries because of the where clause? Like findAndModify does because it returns the whole document before modifying?
Model.where({ _id: id }).update({ title: 'words' })
Nope, but neither does findAndModify, as in both cases the entire command is executed atomically by the MongoDB server.
To confirm, you can see the commands that Mongoose is executing by adding the following to your code:
mongoose.set('debug', true);