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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30204661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:48:10  来源:igfitidea点击:

Insert field with $currentDate to MongoDB collection in Meteor

javascriptmongodbmeteormongodb-query

提问by Mallory-Erik

Not sure how to use $currentDatewhen 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 Dateinstead).

这只能用于更新而不是插入吗?看起来很奇怪,但我没有看到替代方法(除了使用之外new 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

笔记 / 想法 / 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 $currentDatebetter than new Date?
  • One nice thing, if using Moment.js esp, is that $currentDate is entered in ISO 8601 format.
  • Is the answer to do some kind of upsert from the start? If so, could this have unintended consequences?
  • 字段不能以 $ 运算符开头,也不能以花括号开头{}
  • 如果确实是这种情况,那么拥有一个仅适用于更新的运算符有什么意义?
  • 为什么/何时$currentDatenew Date?
  • 如果使用 Moment.js esp,一件好事是 $currentDate 以 ISO 8601 格式输入。
  • 是从一开始就进行某种更新插入的答案吗?如果是这样,这会产生意想不到的后果吗?

回答by styvane

What's the point of having an operator that only works with updates, if that's indeed the case?

如果确实是这种情况,那么拥有一个仅适用于更新的运算符有什么意义?

$currentDateis an update operatorthus you can't use it with the collection.insertmethod. But when upsertis trueit will create a new document when no document matches the query criteria. MongoDB operators tend to follow the Unix philosophy

$currentDate是一个更新运算符,因此您不能将它与该collection.insert方法一起使用。但是当没有文档与查询条件匹配时upserttrue它会在什么时候创建一个新文档。MongoDB 运营商倾向于遵循Unix 哲学

Do One Thing and Do It Well

做一件事并把它做好

So each operator should perform only one task.

所以每个操作员应该只执行一项任务。

Why/when is $currentDatebetter than new Date?

为什么/何时$currentDatenew Date?

First I would like to mention that new Dateis a JavaScriptDate instance.

首先我想提一下这new Date是一个JavaScriptDate 实例。

$currentDateand new Datecan be used when you want to update the value of a field to current date but with new Dateyou need to use another update operatorfor it to work. For example:

$currentDatenew Date当你想一个字段的值更新为当前日期,但有可用于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 }}}
    )
    
  • 使用 new Date

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

    db.collection.update({ "name": "bar"}, 
        { "$currentDate": { "date": { "$type": date }}}
    )
    

Unlike $currentDate, new Datecan be use with the insertmethod and value can be set to a particular if Dateis call with more than on argument.

与 不同$currentDatenew Date可以与insert方法一起使用,并且可以将值设置为特定的 ifDate调用并带有多个 on 参数。

回答by Ludek

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().

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

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

Timestamp granularity is in seconds.

时间戳粒度以秒为单位。

回答by Bhumi Singhal

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

$currentDate 在 $update 构造中使用时可用于插入字段,如果它们不预先存在于文档中。

Quoting documentation for Mongodb 3.4: Behavior

引用 Mongodb 3.4 的文档:行为

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

回答by Monika Mathur

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

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