将带有 $currentDate 的字段插入 Meteor 中的 MongoDB 集合
Insert field with $currentDate to MongoDB collection in Meteor
不确定如何使用 $currentDate 将文档插入 Meteor 中的 MongoDB 集合。
这只能用于更新,不能用于插入吗?看起来很奇怪,但我没有看到替代方案(除了使用 new Date
代替)。
例子
Stuff.insert({
owner: Meteor.userId(),
createdAt: ..., // how to create this field with $currentDate ?
theStuff: "Some of the good stuff"
})
笔记/想法/TL,DR
- 字段不能以 $ 运算符开头,或者据我所知不能以大括号
{}
.
- 如果确实如此,拥有一个仅适用于更新的运算符有什么意义?
- Why/when
$currentDate
比 new Date
好吗?
- 一件好事,如果使用 Moment.js esp,$currentDate 是以 ISO 8601 格式输入的。
- 答案是从一开始就做某种更新插入吗?如果是这样,这会产生意想不到的后果吗?
What's the point of having an operator that only works with updates, if that's indeed the case?
$currentDate
is an update operator thus you can't use it with the collection.insert
method. But when upsert
is true
it will create a new document when no document matches the query criteria. MongoDB operators tend to follow the Unix philosophy
Do One Thing and Do It Well
所以每个操作员应该只执行一项任务。
Why/when is $currentDate
better than new Date
?
首先我想提一下 new Date
是一个 JavaScript 日期实例。
$currentDate
和 new Date
可用于将字段值更新为当前日期但使用 new Date
时需要使用另一个 update operator它工作。例如:
使用new Date
db.collection.update({ "name": "bar" }, { "$set": { "date": new Date() }})
使用$currentDate
db.collection.update({ "name": "bar"},
{ "$currentDate": { "date": { "$type": date }}}
)
与 $currentDate
不同,new Date
可以与 insert
方法一起使用,并且如果 Date
是使用 more than on 参数调用,则可以将值设置为特定值。
您可以从插入操作中创建的自动生成的“_id”中检索时间戳。
http://api.mongodb.com/java/current/org/bson/types/ObjectId.html
Just use the method : ObjectId.getTimestamp().
时间戳粒度以秒为单位。
如果在集合模型中使用 autoValue 会变得更简单
createdAt:
type: Date
autoValue: ->
if this.isInsert
return new Date
else if this.isUpsert
return $setOnInsert: new Date
else
this.unset()
插入或更新数据时会自动设置日期
$currentDate 在 $update 构造中使用时可用于插入字段,如果它们不预先存在于文档中。
Mongodb3.4 的引用文档:
行为
If the field does not exist, $currentDate adds the field to a document.
尽管我完全理解这种设计的合理性,但有一个论点,也许是一个不好的论点,即您希望在插入文档时将 updated_at
字段设置为合理的值。您可能想要这样做的一个原因是避免总是需要查询两个字段来获取最后更新时间。就像我说的,这可能是一个糟糕的 juju,但是你去吧。
无论如何,我能想出的最好的技巧是执行 upsert
过滤 not the empty filter
。这保证 upsert
始终是 insert
.
只是对 MongoDB 开发者的一点评论。作为一个使用数据库多年的人,他们记不得了,让我了解应该如何在 MongoDB:
中正确地做时间戳
created_at => _id.Timestamp
updated_at => $currentDate()
我花了太长时间了。也许那是我的错,可能是,但我认为这是大多数人可能想要/需要做的事情,作为一个概念,它可以得到更好的解释。如果你四处搜索,你会发现很多不好/错误的信息,因为我很确定这是这样做的方式,而且......好吧,互联网还远未就此达成共识(尽管我现在已经达成共识)。
我不知道,也许这是一个测试。也许这是你在招聘 MongoDB 开发人员时问别人的第一件事:你如何做时间戳?嗯,这就是我要问的,因为如果你知道你可能已经在那个阶段学到了大部分 API。
不确定如何使用 $currentDate 将文档插入 Meteor 中的 MongoDB 集合。
这只能用于更新,不能用于插入吗?看起来很奇怪,但我没有看到替代方案(除了使用 new Date
代替)。
例子
Stuff.insert({
owner: Meteor.userId(),
createdAt: ..., // how to create this field with $currentDate ?
theStuff: "Some of the good stuff"
})
笔记/想法/TL,DR
- 字段不能以 $ 运算符开头,或者据我所知不能以大括号
{}
. - 如果确实如此,拥有一个仅适用于更新的运算符有什么意义?
- Why/when
$currentDate
比new Date
好吗? - 一件好事,如果使用 Moment.js esp,$currentDate 是以 ISO 8601 格式输入的。
- 答案是从一开始就做某种更新插入吗?如果是这样,这会产生意想不到的后果吗?
What's the point of having an operator that only works with updates, if that's indeed the case?
$currentDate
is an update operator thus you can't use it with the collection.insert
method. But when upsert
is true
it will create a new document when no document matches the query criteria. MongoDB operators tend to follow the Unix philosophy
Do One Thing and Do It Well
所以每个操作员应该只执行一项任务。
Why/when is
$currentDate
better thannew Date
?
首先我想提一下 new Date
是一个 JavaScript 日期实例。
$currentDate
和 new Date
可用于将字段值更新为当前日期但使用 new Date
时需要使用另一个 update operator它工作。例如:
使用
new Date
db.collection.update({ "name": "bar" }, { "$set": { "date": new Date() }})
使用
$currentDate
db.collection.update({ "name": "bar"}, { "$currentDate": { "date": { "$type": date }}} )
与 $currentDate
不同,new Date
可以与 insert
方法一起使用,并且如果 Date
是使用 more than on 参数调用,则可以将值设置为特定值。
您可以从插入操作中创建的自动生成的“_id”中检索时间戳。
http://api.mongodb.com/java/current/org/bson/types/ObjectId.html
Just use the method : ObjectId.getTimestamp().
时间戳粒度以秒为单位。
如果在集合模型中使用 autoValue 会变得更简单
createdAt:
type: Date
autoValue: ->
if this.isInsert
return new Date
else if this.isUpsert
return $setOnInsert: new Date
else
this.unset()
插入或更新数据时会自动设置日期
$currentDate 在 $update 构造中使用时可用于插入字段,如果它们不预先存在于文档中。
Mongodb3.4 的引用文档: 行为
If the field does not exist, $currentDate adds the field to a document.
尽管我完全理解这种设计的合理性,但有一个论点,也许是一个不好的论点,即您希望在插入文档时将 updated_at
字段设置为合理的值。您可能想要这样做的一个原因是避免总是需要查询两个字段来获取最后更新时间。就像我说的,这可能是一个糟糕的 juju,但是你去吧。
无论如何,我能想出的最好的技巧是执行 upsert
过滤 not the empty filter
。这保证 upsert
始终是 insert
.
只是对 MongoDB 开发者的一点评论。作为一个使用数据库多年的人,他们记不得了,让我了解应该如何在 MongoDB:
中正确地做时间戳created_at => _id.Timestamp
updated_at => $currentDate()
我花了太长时间了。也许那是我的错,可能是,但我认为这是大多数人可能想要/需要做的事情,作为一个概念,它可以得到更好的解释。如果你四处搜索,你会发现很多不好/错误的信息,因为我很确定这是这样做的方式,而且......好吧,互联网还远未就此达成共识(尽管我现在已经达成共识)。
我不知道,也许这是一个测试。也许这是你在招聘 MongoDB 开发人员时问别人的第一件事:你如何做时间戳?嗯,这就是我要问的,因为如果你知道你可能已经在那个阶段学到了大部分 API。