gpt4 book ai didi

node.js - 与 Mongoose 的日期比较

转载 作者:IT老高 更新时间:2023-10-28 13:15:29 28 4
gpt4 key购买 nike

我正在尝试查找某个用户的位置比某个日期新或旧的用户。

架构

var userSchema = new Schema({
firstName: String,
lastName: String,
email: String,
facebookId: String,
googleId: String,
token: {type: String, unique: true, required: true},
friends: [userSchema],
location: {
type: {
timestamp: Date,
accuracy: Number,
coordinates: {
lat: Number,
lng: Number
}
},
default: null
}
}, {

查询

userSchema.statics.findRecentUpdates = function (ids, minutesSinceLast, callback) {
var recentDate = new Date().removeMinutes(minutesSinceLast);
this.find(
{'location.timestamp': {$lt: recentDate}},
callback
);
}

数据

db.users.find();
{ "__v" : 1, "_id" : ObjectId("53cecb60fe7d090000030328"), "email" : "JzLrmXy@givnZhE.com", "facebookId" : "9138129367", "firstName" : "Cornel", "friends" : [ ObjectId("53cecb4dfe7d090000030327") ], "lastName" : "GD", "location" : { "coordinates" : { "lat" : 18.040808, "lng" : 59.330557 }, "timestamp" : 1406061408964, "accuracy" : 1 }, "token" : "988bb52b-cc0c-492d-8c75-f1f7d882157d" }
{ "__v" : 20, "_id" : ObjectId("53cf668eff9ef40000f76ce5"), "email" : "CZteADE@WFdFFiQ.com", "facebookId" : "5762365752", "firstName" : "Yuan", "friends" : [ ], "lastName" : "vh", "location" : { "accuracy" : 3, "coordinates" : { "lat" : 59.356862, "lng" : 17.996661 }, "timestamp" : 1406101134199 }, "token" : "51278cb7-9e55-4cc5-b635-7cd289b2fa6a" }
{ "__v" : 21, "_id" : ObjectId("53cecb4dfe7d090000030327"), "email" : "s@gmail.com", "facebookId" : "7237853051", "firstName" : "M", "friends" : [ ObjectId("53cecb60fe7d090000030328") ], "lastName" : "H", "location" : { "accuracy" : 0, "coordinates" : { "lng" : 18.0246476, "lat" : 59.3827026 }, "timestamp" : 1413838822856 }, "token" : "5d870480-05c0-4a83-ae44-cfe39692d308" }

无论我将日期设置为什么,我总是得到一个空结果。尝试使用 'location.accuracy' 并设置小于或大于可行的值。

最佳答案

错误在于我存储日期的方式。正如我们从上面的数据中看到的,它们以毫秒为单位存储 1406101134199 而不是 ISODate 格式 ISODate("2014-10-20T21:50:23.196Z")。这是由于行 Date.now() 不返回当前日期对象,而是返回毫秒。使用 new Date() 修复了错误。

关于node.js - 与 Mongoose 的日期比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26475013/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com