KMongo queries producing different results from Mongo Shell - mongodb

I'm currently using KMongo and it's very nice. Simple to use, nice syntax, etc.
But I ran into an issue while querying some data that I cannot figure it out. I'm filtering for some fields and when I run my queries on Mongo Shell or robo 3T it works fine. Nevertheless, when I try to run on my Kotlin application, it (only in some cases) does not work. I'm querying like this:
collection.find(
MyEntity::name regex filter.name,
MyEntity::role eq filter.role,
)
But I also tried writing a string with the native query receiving the filtering value and I had the same issue. A concrete example is this query:
{ 'role': 'VENDOR', 'name': 'Internal Revenue Service'}
If I run on robo 3T like this:
db.getCollection('MyEntity').find({ 'role': 'VENDOR', 'name': 'Internal Revenue Service'})
I receive the results I expect, but if I run with KMongo, the exactly same query (and I doubled checked with the debugger), I receive nothing in result:
collection.find("{ 'role': 'VENDOR', 'name': 'Internal Revenue Service'}")
When I use regex to query (like in the first example), it seems to return only with small values: if I query for name with 'Internal Revenue Service' it produces no result, but if I query with only 'Internal' the result is correct.
Does anyone have any idea of what it could be? It seems deadly simple, but it's killing me that I can't figure it out.

I found out one day later that there was an injection of an offset that was wrongfully computed. So, when the query was more specific, it returned nothing.
That was kind of sad.

Related

MONGODB: $in operator not matching any record

community!
I am in a weird situation. The direct equally check returns result, but when using $in I am not getting any records.
db.getCollection("voter").find({"id":{$in:["db1eefc5-09ad-4d4f-a31a-db63d8261913"]}})
db.voter.find({"id":{$in:["db1eefc5-09ad-4d4f-a31a-db63d8261913"]}})
Doesn't return anything.
db.voter.find({id: "db1eefc5-09ad-4d4f-a31a-db63d8261913"})
Returns the desired record.
Being more of a fullstack developer, I don't know what's happening in-depth, but I am sure that both things shall work ideally which is not the case here.
Extra info:
I have defined hashed unique indexes on id.
Thanks.
The problem is pretty simple:
On the first screen you're running your query against admin database
while second query gets executed against crmadmin db

mongoDB spring-boot custom query in uppercase, with LIKE

I need to search the items by their names via spring boot on a db implemented by mongodb. For a normal SQL database I would've done as follows:
#Query("SELECT * FROM customer WHERE UPPER(name) LIKE UPPER(CONCAT('%', :name, '%'))")
List<Customer> findByName(#Param("name") String name);
Yet it does not accept a normal 'SELECT' query, because it is a NoSQL database. Thus I need to query this collection by using JSON format, yet I have no idea of the implementation on spring-boot.. This is my first time using mongodb too.
Going through the posts on stackoverflow, I have found an example such as:
#Query("{'name':?0}")
And on this site there are examples. Also here there is an explanation of the query above. Yet still I have no idea how to convert the former query I have pasted above into a "json based query".
Note: I am extending MongoRepository on my repository.
Note2: The above query is case sensitive, that's the reason I am trying to query in upper case along with converting the column data into upper case.
UPDATE
#Query(" { $text: { $search: ?0 , $caseSensitive: false } }")
works perfectly(I had to index my collection by launching this command:db.customerCollection.createIndex({name: "text"}) ) to ignore case sensitivity, but I still need to implement 'LIKE'; because when I search 'user', it is able to find 'UsEr' etc. But if I search 'se', it doesn't bring me any result. Any help will be appreciated!
Thank you for your time!
I have resolved my issue by using $regex and writing the query as:
#Query(" { companyName: { $regex : '(?i)?0'} } }")
I would've never guessed to spend that much time for such a simple thing. But I am glad that it is resolved anyway !
[ref]

How to insert multiple records with OrientJS?

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?

pymongo sort grouped results

I need to group and sort by date_published some documents stored on mongodb using pymongo.
the group part went just fine :) but when I'm addding .sort() to the query it keeps failing no matter what I tried :(
here is my query:
db.activities.group(keyf_code,cond,{},reduce_code)
I want to sort by a field called "published" (timestamp)
tried to do
db.activities.group(keyf_code,cond,{},reduce_code).sort({"published": -1})
and many more variations without any success
ideas anyone?
You can't currently do sort with group in MongoDB. You can use MapReduce instead which does support a sort option. There is also an enhancement request to support group with sort here.
Although MongoDB doesn't do what you want, you can always use Python to do the sorting:
result = db.activities.group(keyf_code,cond,{},reduce_code)
result = sorted(result, key=itemgetter("published"), reverse=True)

MongoDB query: field either does not exist or has specific value

I'd like to query a mongo collection for records which either don't have a value for a field named 'scheme', or explicitly have the value 'http' for 'scheme'. Sounds pretty easy, but this problem has proved more complex than it first appears.
Since db.collection.find({'scheme': None}) returns all records where 'scheme' is undefined (no index field), I initially assumed the following would work:
db.collection.find({'scheme': {'$in': ['http', None]}})
However, this seems to exclude values in which 'scheme' is undefined, so I can only assume it is searching for records where scheme is either 'http', or explicitly defined to be None. This seems to be a bit counterintuitive, but there we have it. My second attempt was the following:
db.collection.find( {'$or': [{'scheme': {'$exists': False}}, {'scheme': 'http'}]})
This also excludes result where scheme is undefined. This time, I can't even think of a logical reason why this is failing.
Any ideas why this is failing, and how I can get it to work as desired?
Thanks
EDIT: Just thought I'd note that I'm performing this query through Python (pymongo), which explains the None (over Javascript's null)
Resolved: This is apparently an issue of my version of mongodb (1.4.4), the issue is solved in 1.6.5.