Adding condition on a date field in PDI - mongodb

I am using PDI to extract data from the database. I am having a problem where i cannot add a condition in date field as it always gives error.
I have tried following ways
{news_date: {$gte: ISODate("2020-12-30")} }
{news_date: {$gte: new Date("2020-12-30")} }
nothing is working and I am always getting errors. Any help in this regard will be much appreciated. How do I pull data from mongo DB filtered on Date?
following is the error i am getting on preview which also makes no sense to me

This works for me:
{ news_date: { $gte: ISODate("2020-12-30T00:00:00.000Z")} }
But how the stored news_date looks like?

Related

mongodb compass query with objectId in date range

I'm trying to perform a date range query on the _id field using compass.
I've tried what I found here with the following filter:
{_id: { $gte: ObjectId.fromDate(new Date('2019-01-01')) } }
What am I missing? I'd like to get a list of all documents from some date forward (in this example from 1 Jan 2019 to present). Unfortunately there isn't a timestamp in the document fields so I need to extract it from the object id.
You need to pass a date object to the ObjectId.fromDate, not a string. Try this:
ObjectId.fromDate(new Date('2019-01-01'))
This function works only in the shell and doesn't exist in the drivers.
EDIT after comments:
Here is a solution that works in Compass as well:
{
$expr: {
$gte: [
{"$toDate":"$_id"},
ISODate("2021-01-01T00:00:00.000Z")
]
}
}
Keep in mind, however, that it requires a version of mongo of 4.0+. You can checkout the docs here.
Also, checkout this related topic: Can I query MongoDB ObjectId by date?
It is not about Compass, but it provides solutions for generating the ObjectIds from a date without being dependent on ObjectId.fromDate().
I found a solution, maybe not the best but it does the job. Hopefully this can help someone with the same problem if there isn't a cleaner solution.
I converted the date I needed to an ObjectId outside of compass online here.
Then I wrote the query with that ObjectId:
{_id: { $gte: ObjectId(' object id here ') } }
As suggested in the comments, see this related topic. It's not specific to Compass but it provides a solution for generating ObjectIds from a date without being dependent on ObjectId.fromDate().

Change field type for many documents in MongoDb

I'm learning MongoDb, I have many documents like this in my collection:
And I want to update my cust_id field from String to ObjectId. I can use Compass to change type but I have very many documents. I've read from Mongo document but still don't understand. Can you show me how to do this on my document for me to understand clearly.
Thank you very much!
There are a few posts here that hint at the solution.
db.students.find().forEach(function(obj) {
obj.cust_id = ObjectId(obj.cust_id);
db.students.save(obj);
});
I tested this in Atlas 4.0.6 and it worked well. I don't know, however, how well this technique will scale to a large dataset.
Just replace students with your own collection name.
I have tested this function turning a string into number and it worked well
db.students.find().forEach(obj =>{
obj.code_number= parseInt(obj.code_number);
db.students.save(obj);
});
You can also create a new collection with the results to keep the original data and check the results
db.students.find().forEach(obj=> {
obj.code_number= parseInt(obj.code_number);
db.students_new.save(obj);
});

Cannot update a document

I am facing some problems trying to update a document. It is a simple update command but it didn't save any changes. So. I am looking for some advice.
This is the query:
db.Customers_DEV.update(
{"fn": "QA_Isabel10K"},
{
$set: {
"db" : ISODate("1988-08-17T06:00:00.000Z")
}
});
The document does exist and it throws the next message as a result:
Updated 0 record(s) in 50ms
Also tried by right clicking on the document and "Edit Document..." but here it throws the following error:
Error when saving document: 1 Not primary while performing update on [database].
I am using Robomongo 0.9.0-RC10
Thanks in advance
UPDATE
This is the document:
Document to modify
Solved. Posting here for future reference.
I am not pretty sure how mongodb/robomongo works but the problem was the port number on my connection string. It unable me to make any insert nor update on the database.
Thanks and hope this help somebody else.
for me, it helped to update Robo3T from 1.4 to 1.4.4

Sorting data ($sort) in quer documents dialog of adminMongo

Using adminMongo as web-ui for MongoDB, I want to filter and order the documents. Filtering works simple:
{
"status": 4
}
But additionally, all documents should be sorted by date field, which is a unix timestamp. In MySQL I would simply add ORDER BY date DESC but as a newbie, It's not clear for me how to do this in adminMongo.
I took a look in the documentation. They execute js code with methods. To get in this, I used the shell and could get my expected data result using the following code:
db.getCollection('my-collection').find({"status": 4}).sort({date: -1})
However this works on the shell, it creates an error in adminMongo. Seems that adminMongo expects a json document. So we've 2 syntax variants? It's confusing me, since on SQL we've SQL and don't care if we write SQL in any programming language, admin ui or directly on the shell.
I think I've to convert the find method to json, but don't know why. According to the docs, we can use $sort as $sort: { 'date': -1}. Calling aggreagate() method on the collection, this works. On adminMongo, I got an error when trying to send something like
{
{ "$sort": { 'date': -1} }
}
What is the right syntax which I need to use on adminMongo? Where is documented, how I can convert methods like aggregate which are used for sorting, to this syntax?

What is the fastest way to see when the last update to MongoDB was made

I'm writing a long-polling script to check for new documents in my mongo collection and the only way I know of checking to see whether or not changes have been made in the past iteration of my loop is to do a query getting the last document's ID and parsing out the timestamp in that ID and seeing if it's greater than the timestamp left since I last made a request.
Is there some kind of approach that doesn't involve making a query every time or something that makes that query the fastest?
I was thinking a query like this:
db.chat.find().sort({_id:-1}).limit(1);
But it would be using the PHP driver.
The fastest way will be creating indexes on timestamp field.
Creating index:
db.posts.ensureIndex( { timestamp : 1 } )
Optimizes this query:
db.posts.find().sort( { timestamp : -1 } )
findOne give you only one the last timestamp.
nice to help you.
#Zagorulkin Your suggestion is surely going to help in the required scenario. However i don't think so sort() works with findOne().