Mongodb string to date, problem with null values [duplicate] - mongodb

This question already has answers here:
MongoDB: How to query for records where field is null or not set?
(9 answers)
Update MongoDB field using value of another field
(12 answers)
How do you query for "is not null" in Mongo?
(11 answers)
Closed 3 years ago.
I have a MongoDB (v 3.4.19) installed and running. After importing the data in .JSON format, one of the fields in the collection is recognised as string instead of the proper date or datetime format. I try to convert the fields, but what is my surprise after discovering that the fields with null values are filled with random (?) dates. I tried many different approaches, all of them without success. I wonder if any of you can give me a hand with this issue.
This is the code I have been using until now:
db.MIDAS_weather.find()
.batchSize(1000)
.forEach(function(doc) {
doc.METO_STMP_TIME=new Date(doc.METO_STMP_TIME);
db.MIDAS_weather.save(doc);
});
After importing the JSON files, the null values are in the right place. Is only after attempting the conversion when the data is messed.

Related

How to fetch the data on the basis of multiple ids? [duplicate]

This question already has answers here:
Querying mongodb from golang using the _id stored in an array
(3 answers)
Find by id with mgo
(1 answer)
Closed 4 years ago.
Here I'm fetching the data from the database mongodb using golang. Suppose I have an Id attribute and i want to receive the data on the basis of this id and it was easy by passing the id attribute in the query like below:-
c.Find(bson.M{"_id": 1}).One(&data)
If the id is given 1. but I have an case that i have find two or more at one time on the basis of the ids that it will returns the array of the ids like "_id":[1,2,3] then How will I retrieve the data from that ids. Can anyone help for if.
EDITED
IS this question solve my problem.
Thanks

How to display records in descending order in mongodb [duplicate]

This question already has answers here:
How to do alphanumeric sort in mongoDB?
(2 answers)
Closed 4 years ago.
I have a table in MongoDB in which I can get the fields in descending order
sorted by system_id
db.job_parameters_mongo.find().sort({system_id : -1})
This is fine but gives me incorrect result as system_id is string field.Is there any way that I can convert system_id to number before ordering in descending order.I saw on this site that there is a way to do this using forEach (how to convert string to numerical values in mongodb) but that uses a function.Isn't there any other way like we have to_number in rdbms?
You can use $toInt available in 4.0 version.
db.job_parameters_mongo.aggregate([
{"$addFields":{"system_id":{"$toInt":"$system_id"}}},
{"$sort":{"system_id":-1}}
])

find data from all field in single table mongodb [duplicate]

This question already has answers here:
Mongodb text search multiple fields
(2 answers)
mognoose searching with regular expression in multiple columns
(2 answers)
Closed 5 years ago.
Hello I am new at mongodb , I have made server side pagination , working perfect, I have one search textbox in which I type anithing like email , name , number etc it should give me result but I am facting issue in serverside searching data from all field of single table , can anyone give me idea how can I do that

How can I apply a filter in MongoDB find operation to check empty string? [duplicate]

This question already has answers here:
Test empty string in mongodb and pymongo
(4 answers)
Closed 5 years ago.
I am working on a application built in PHP using MongoDB as a database.
Data is organized across BSON documents into a collection in MongoDB.
I need to retrieve only those documents where field containing string value is non empty value. I searched for functions equivalent to empty and strlen functions belonging to PHP language but did not get any relevant search results.
try this,
$cursor = $collection->find(array("someField" => array('$ne' => null)));

How to compare 2 ISODate fields in Mongo? [duplicate]

This question already has answers here:
How to compare two strings in mongoDB spring data?
(1 answer)
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 5 years ago.
I have 2 fields like this in my document
"DATE_1" : ISODate("2017-08-11T04:00:00Z")
"DATE_2" : ISODate("2017-06-12T04:00:00Z")
I would like to select documents with "DATE_1" is greater than "DATE_2" and i tried the following query which should return the above document.But its not giving any result
db.collection.find({"DATE_1":{$gte:"DATE_2"}})
How do I compare 2 ISODate fields in Mongo? Also how do I make this query using Spring date mongodb? something like this?
Criteria.where("DATE_1").gte("DATE_2");
This should work. Can you please try this and give your feedback.
db.getCollection('collectionName').find({$where: function() {
return this.DATE_1 > this.DATE_2;
}})