将$ currentDate的字段插入Meteor中的MongoDB集合

时间:2021-10-28 16:39:10

Not sure how to use $currentDate when inserting a document into a MongoDB collection in Meteor.

在Meteor中将文档插入MongoDB集合时,不确定如何使用$ currentDate。

Can this only be used in an update, not an insert? Would seem strange, but I don't see an alternative (other than using new Date instead).

这只能用于更新,而不是插入吗?看起来很奇怪,但我没有看到另一种选择(除了使用新的Date)。

Example

Stuff.insert({ 
   owner: Meteor.userId(),
   createdAt: ..., // how to create this field with $currentDate ?
   theStuff: "Some of the good stuff"
})

Notes / Thoughts / TL,DR

Notes / Thoughts / TL,DR

  • Fields can't start with $ operators or, as far as I know, curly braces {}.
  • 字段不能以$运算符开头,或者据我所知,大括号{}。

  • What's the point of having an operator that only works with updates, if that's indeed the case?
  • 如果确实如此,那么拥有仅适用于更新的运营商有什么意义呢?

  • Why/when is $currentDate better than new Date?
  • 为什么/什么时候$ currentDate比新Date更好?

  • One nice thing, if using Moment.js esp, is that $currentDate is entered in ISO 8601 format.
  • 如果使用Moment.js esp,一个好处是$ currentDate以ISO 8601格式输入。

  • Is the answer to do some kind of upsert from the start? If so, could this have unintended consequences?
  • 答案是从一开始就做某种事吗?如果是这样,这会产生意想不到的后果吗?

4 个解决方案

#1


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

$ currentDate是一个更新操作符,因此您不能将它与collection.insert方法一起使用。但是当upsert为true时,当没有文档符合查询条件时,它将创建一个新文档。 MongoDB运营商倾向于遵循Unix哲学

Do One Thing and Do It Well

做一件事并做得好

So each operator should perform only one task.

因此每个操作员只应执行一项任务。

Why/when is $currentDate better than new Date?

为什么/什么时候$ currentDate比新Date更好?

First I would like to mention that new Date is a JavaScript Date instance.

首先,我想提一下,新Date是一个JavaScript Date实例。

$currentDate and new Date can be used when you want to update the value of a field to current date but with new Date you need to use another update operator for it to work. For example:

当您想要将字段的值更新为当前日期但使用新日期时需要使用另一个更新运算符才能使用$ currentDate和new Date。例如:

  • Using new Date

    使用新日期

    db.collection.update({ "name": "bar" }, { "$set": { "date": new Date() }})
    
  • Using $currentDate

    db.collection.update({ "name": "bar"}, 
        { "$currentDate": { "date": { "$type": date }}}
    )
    
  • 使用$ currentDate db.collection.update({“name”:“bar”},     {“$ currentDate”:{“date”:{“$ type”:date}}} )

Unlike $currentDate, new Date can be use with the insert method and value can be set to a particular if Date is call with more than on argument.

与$ currentDate不同,new Date可以与insert方法一起使用,如果使用多于on参数调用Date,则可以将值设置为特定值。

#2


You can retrieve timestamp from autogenerated "_id" that is created within insert operation.

您可以从插入操作中创建的自动生成的“_id”中检索时间戳。

http://api.mongodb.com/java/current/org/bson/types/ObjectId.html

Just use the method : ObjectId.getTimestamp().

只需使用方法:ObjectId.getTimestamp()。

Timestamp granularity is in seconds.

时间戳粒度以秒为单位。

#3


It will become more simple if you will use autoValue in collection model

如果你在集合模型中使用autoValue会变得更简单

createdAt:
type: Date
autoValue: ->
  if this.isInsert
    return new Date
  else if this.isUpsert
    return $setOnInsert: new Date
  else
    this.unset()

It will automatically set date while insert or update the data into it

它会在插入或更新数据时自动设置日期

#4


$currentDate when used in the $update construct can be used to insert fields if they do not pre-exist in the document.

$ current结构中使用的$ currentDate可用于插入字段(如果它们在文档中不存在)。

Quoting documentation for Mongodb 3.4: Behavior

引用Mongodb 3.4的文档:行为

If the field does not exist, $currentDate adds the field to a document.

#1


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

$ currentDate是一个更新操作符,因此您不能将它与collection.insert方法一起使用。但是当upsert为true时,当没有文档符合查询条件时,它将创建一个新文档。 MongoDB运营商倾向于遵循Unix哲学

Do One Thing and Do It Well

做一件事并做得好

So each operator should perform only one task.

因此每个操作员只应执行一项任务。

Why/when is $currentDate better than new Date?

为什么/什么时候$ currentDate比新Date更好?

First I would like to mention that new Date is a JavaScript Date instance.

首先,我想提一下,新Date是一个JavaScript Date实例。

$currentDate and new Date can be used when you want to update the value of a field to current date but with new Date you need to use another update operator for it to work. For example:

当您想要将字段的值更新为当前日期但使用新日期时需要使用另一个更新运算符才能使用$ currentDate和new Date。例如:

  • Using new Date

    使用新日期

    db.collection.update({ "name": "bar" }, { "$set": { "date": new Date() }})
    
  • Using $currentDate

    db.collection.update({ "name": "bar"}, 
        { "$currentDate": { "date": { "$type": date }}}
    )
    
  • 使用$ currentDate db.collection.update({“name”:“bar”},     {“$ currentDate”:{“date”:{“$ type”:date}}} )

Unlike $currentDate, new Date can be use with the insert method and value can be set to a particular if Date is call with more than on argument.

与$ currentDate不同,new Date可以与insert方法一起使用,如果使用多于on参数调用Date,则可以将值设置为特定值。

#2


You can retrieve timestamp from autogenerated "_id" that is created within insert operation.

您可以从插入操作中创建的自动生成的“_id”中检索时间戳。

http://api.mongodb.com/java/current/org/bson/types/ObjectId.html

Just use the method : ObjectId.getTimestamp().

只需使用方法:ObjectId.getTimestamp()。

Timestamp granularity is in seconds.

时间戳粒度以秒为单位。

#3


It will become more simple if you will use autoValue in collection model

如果你在集合模型中使用autoValue会变得更简单

createdAt:
type: Date
autoValue: ->
  if this.isInsert
    return new Date
  else if this.isUpsert
    return $setOnInsert: new Date
  else
    this.unset()

It will automatically set date while insert or update the data into it

它会在插入或更新数据时自动设置日期

#4


$currentDate when used in the $update construct can be used to insert fields if they do not pre-exist in the document.

$ current结构中使用的$ currentDate可用于插入字段(如果它们在文档中不存在)。

Quoting documentation for Mongodb 3.4: Behavior

引用Mongodb 3.4的文档:行为

If the field does not exist, $currentDate adds the field to a document.