i want to use this query but not working can you help me
dbm.collection("tasks").remove({ updatedAt: { $lt: anotherDate } })
in database => updatedAt: "Mon Jul 11 16:42:33 GMT+04:30 2022"
anotherDate:2022-07-11T13:34:26.838Z
Related
I have create a mock data and performed a batch update with firestore.batch(). The problem is that location is saved as a number and time as a string and not as Geopoint and Timestamp instances respectively, causing my React app to crash.
I could see that only firestore is accessible through window.firestore, but not Firebase. Since Firebase isn't exported as window.firebase, I cannot create either Geopoint or Timestamp instance.
So, how to create a Timestamp and Geopoint instance in Firebase Emulator from browser console?
Here's a type of doc I'm adding to firestore
const doc = {
"company": "company-1",
"location": [
-72.3623, // number
79.5748 // but, want to convert to Geopoint instance
],
"time": "Fri Sep 10 1976 07:42:23 GMT+0530 (India Standard Time)", // string
"createdAt": "Mon Apr 28 2014 13:30:16 GMT+0530 (India Standard Time)", // want to convert to Timestamp
}
A more dependable accessor path than .Gf:
new firestore.app.firebase_.firestore.GeoPoint(lat, lng)
There's a firebase accessible through window.firestore.
So I figured a way to create Geopoint and Timestamp instance through Firestore.
You can access it through window.firestore.Gf.firebase_, through which you can create those both instance.
const raw = // pasting from clipboard
const batch = firestore.batch()
const firebase = firestore.Gf.firebase_
const Timestamp = firebase.firestore.FieldValue().Timestamp
const GeoPoint = firebase.firestore.FieldValue().GeoPoint
raw.forEach(doc => {
const docRef = firestore
.collection('user')
.doc('user-1')
.collection('interviews')
.doc()
doc = {
...doc,
time: Timestamp.fromDate(new Date(doc.time)),
createdAt: Timestamp.fromDate(new Date(doc.createdAt)),
location: new GeoPoint(doc.location[0], doc.location[1])
}
batch.set(docRef, doc)
})
batch.commit()
I'm new to mongoose. I'm trying to query by createdAt date, with startDate and endDate, however I got the incorrect number of results.
data
{"_id":{"$oid":"5f4fab9beceaa20f898feafb"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Alex","email":"alex#gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-02T14:26:35.237Z"},"updatedAt":{"$date":"2020-09-02T14:26:35.237Z"}}
{"_id":{"$oid":"5f4fc3677e7b1e2d806714cf"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Joshua","email":"joshua#gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-02T16:08:07.123Z"},"updatedAt":{"$date":"2020-09-02T16:08:07.123Z"}}
{"_id":{"$oid":"5f50b80f28ca26065b2ac9a5"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Harold","email":"harold#gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-03T09:31:59.112Z"},"updatedAt":{"$date":"2020-09-03T09:31:59.112Z"}}
{"_id":{"$oid":"5f59104ff518c40579b578d0"},"message":"Inquiry 101","service":"GENERAL_INQUIRY","name":"Katy","email":"katy#gmail.com","personalNumber":"0991898838398","createdAt":{"$date":"2020-09-09T17:26:39.787Z"},"updatedAt":{"$date":"2020-09-09T17:26:39.787Z"}}
I have 4 records with the ff. date 2020-09-02, 2020-09-03 and 2020-09-09
I wanted to get all records from 2020-09-02 and 2020-09-03, with these I expected 3 results as I have to records on the 2020-09-02, however I only got 2 results, those records have 2020-09-02 date with them.
const { limit = 30 } = params;
return new Promise((resolve, reject) => {
const query = {
createdAt: {
$gte: '2020-09-02',
$lte: '2020-09-03',
}
};
this.model.find(query).sort({
createdAt: 'descending',
}).limit(limit).exec((err, res) => {
if (!err) {
resolve(res);
}
reject(err);
})
})
Did I miss something with my code?
I also tried passing new Date('2020-09-02') but I still got same results.
I tried setting mongoose debug to true and below is what I got.
Mongoose: inquiries.find({ createdAt: { '$gte': new Date("Wed, 02
Sep 2020 00:00:00 GMT"), '$lte': new Date("Thu, 03 Sep 2020 00:00:00 GMT") }}, { sort: { createdAt: -1 }, limit: 30, projection: {}
})
Thanks in advance.
Youre looking for records greater than 2020-09-02 00:00:00 and less than 2020-09-03 00:00:00.
You only have 2 records which are between these values, if you want records including those at 2020-09-03 23:59:59, set your lte to 2020-09-04
I'm using a snapshot test in my project and came across a weird problem when running this specific test on a CI server: it displays the timezone name instead of the GMT code, causing the test failure.
I have tried using "moment-timezone" and Date.UTC() to normalize the dates, the result shown was the correct date with the same issue as above.
I've also tried to stub the global.Date object, but the components complained about prop incompatibility.
it('should render with props', () => {
const order = {
merchant: { logo: 'abc', name: 'Pizza Hut' },
bag: {
items: [{ name: 'Corn & Bacon' }],
total: {
valueWithDiscount: 99.99,
},
},
delivery: {
deliversAt: new Date('2019-05-21 13:00'),
},
payment: {
mode: 'online',
},
lastStatus: API_STATUSES.cancelled,
createdAt: new Date('2019-05-21 12:00'),
details: {},
};
const wrapper = shallowMount(Order, {
...commons,
propsData: { order },
});
expect(wrapper).toMatchSnapshot();
});
See that the expected date is the same as the received one, but syntactic differences:
<div class="order__details">
- <orderdetails-stub paymentmode="online" deliverytime="Fri Jun 21 2019 10:00:00 GMT-0300 (GMT-03:00)" value="99.99" laststatus="cancelled"></orderdetails-stub>
+ <orderdetails-stub paymentmode="online" deliverytime="Fri Jun 21 2019 10:00:00 GMT-0300 (Brasilia Standard Time)" value="99.99" laststatus="cancelled"></orderdetails-stub>
Using Date strings as props like this is hazardous and likely to lead to the sort of problem you're encountering.
Best practice for tests in my experience is to use Date.getTime() so values are numbers of milliseconds without any locale information.
Alternatively, you can use moment-timezone as described in this article:
import moment from 'moment-timezone';
it('renders without crashing', () => {
moment.tz.setDefault('EST');
let props = {
currentDay: moment("2017-09-15 09:30:00").format("MMM Do YYYY h:mm:ss a")
};
const tree = renderer.create(<App {...props} />).toJSON();
expect(tree).toMatchSnapshot();
});
I am processing data read from database on the server using the following code:
module.exports = mongoose.model('Todo', {
text : {type : String, default: ''},
created_at : Date
});
var processTodos = function ( todos ){
for (var i = todos.length - 1; i >= 0; i--) {
// Following update is not happening
todos[i].created_at = "Custom date";
};
console.dir(todos);
return todos;
};
I am not able to figure out how to update this. Is there a syntax issue that is causing this.
I am using MEAN stack for my application.
// Following update is not happening
todos[i].created_at = "Custom date";
What am i missing here.
Here is the console log for "console.dir(todos);":
{ _id: 5489dda3f23f159400475dba,
created_at: Thu Dec 11 2014 23:38:35 GMT+0530 (India Standard Time),
__v: 0,
text: 'Testing sorting at server side' }
{ _id: 5489ddacf23f159400475dbb,
created_at: Thu Dec 11 2014 23:38:44 GMT+0530 (India Standard Time),
__v: 0,
text: 'It works' }
{ _id: 5489f31a12fa54cc127f3e1d,
created_at: Fri Dec 12 2014 01:10:10 GMT+0530 (India Standard Time),
__v: 0,
text: 'time to add more data' }
If you'd like to save the changes you're making to your object, you need to persist the change using the .save() method like so:
var processTodos = function ( todos ){
for (var i = todos.length - 1; i >= 0; i--) {
// Following update is not happening
todos[i].created_at = "Custom date";
todos[i].save();
};
console.dir(todos);
return todos;
};
I have an extra question based on the one I asked before:
calculate frequency using mongodb aggregate framework
so my data in MongoDB looks like this now:
{
"data": {
"interaction": {
"created_at": "Wed, 09 Apr 2014 14:38:16 +0000"
}
},
"_id": {
"$oid": "53455b59edcd5e4e3fdd4ebb"
}
}
before I used to have it like:
[
{
created_at: "2014-03-31T22:30:48.000Z",
id: 450762158586880000,
_id: "5339ec9808eb125965f2eae1"
}
]
so to access created_at I was using mapper like:
var mapper = function () {
if ( this.created_at.getTime() > ( last_date + 10000 ) ) {
...
but as the structure in my database has changed, I tried to change:
this.created_at.getTime()
to:
this.data.interaction.created_at.getTime()
but unfortunately it didn't work out. Thank you for any help
Hate to make this that simple but all you want to do when importing these date strings is this:
new Date("Wed, 09 Apr 2014 14:38:16 +0000")
Which will return a proper date type that you actually should be inserting as part of your data.