简体   繁体   English

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

[英]Insert field with $currentDate to MongoDB collection in Meteor

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).看起来很奇怪,但我没有看到替代方案(除了使用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 $currentDate better than new Date ?为什么/什么时候$currentDatenew 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?从一开始就做某种 upsert 的答案是什么? If so, could this have unintended consequences?如果是这样,这会产生意想不到的后果吗?

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. $currentDate是一个更新操作符,因此您不能将它与collection.insert方法一起使用。 But when upsert is true it will create a new document when no document matches the query criteria. 但是当upserttrue时,当没有文档符合查询条件时,它将创建一个新文档。 MongoDB operators tend to follow the Unix philosophy 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 ? 为什么/什么时候$currentDatenew Date更好?

First I would like to mention that new Date is a JavaScript Date instance. 首先,我想提一下, new 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. 当您想要将字段的值更新为当前日期但使用new Date时需要使用另一个更新运算符才能使用$currentDatenew Date For example: 例如:

  • Using new Date 使用new Date

     db.collection.update({ "name": "bar" }, { "$set": { "date": new Date() }}) 
  • Using $currentDate 使用$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则可以将值设置为特定值。

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 http://api.mongodb.com/java/current/org/bson/types/ObjectId.html

Just use the method : ObjectId.getTimestamp(). 只需使用方法:ObjectId.getTimestamp()。

Timestamp granularity is in seconds. 时间戳粒度以秒为单位。

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 它会在插入或更新数据时自动设置日期

$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.

Although I do totally appreciate that this design makes perfect sense, there is an argument, perhaps a bad one, that you want to set the updated_at field to something sensible when you insert a document.尽管我非常欣赏这种设计的完美意义,但有一个论点,也许是一个不好的论点,即您希望在插入文档时将updated_at字段设置为合理的值。 One reason that you might want to do this is to avoid always needing to query on two fields to get the last updated time.您可能想要这样做的一个原因是避免总是需要查询两个字段来获取最后更新时间。 Like I say, this might be bad juju, but well there you go.就像我说的,这可能是一个糟糕的 juju,但是你 go。

Anyway the best hack that I could come up with to do this is to perform an upsert that filters on not the empty filter .无论如何,我能想出的最好的 hack 是执行一个upsert过滤而not the empty filter That guarantees that the upsert is always an insert .这保证了upsert始终是insert

Just a small comment on this to the MongoDB Devs.只是对 MongoDB 开发人员的一点评论。 As someone who has been working with databases for more years than they care to remember, getting my head around how one is supposed to do timestamps correctly in MongoDB:作为一个使用数据库多年的人,他们记不得了,让我思考一下应该如何在 MongoDB 中正确地做时间戳:

created_at => _id.Timestamp
updated_at => $currentDate()

has taken me far too long.我花了太长时间了。 Maybe that's my fault, it probably is, but it's something that I think most people probably want / need to do and as a concept it could be explained better.也许那是我的错,可能是,但我认为这是大多数人可能想要/需要做的事情,作为一个概念,它可以得到更好的解释。 If you search around you will find a lot of bad / wrong information, because I'm pretty sure that this is the way to do it and... well the inte.net is far from at consensus on this (although I am now).如果你四处搜索,你会发现很多不好的/错误的信息,因为我很确定这是这样做的方式......好吧,inte.net 还远未就此达成共识(尽管我现在).

I don't know, maybe it's a test.不知道,可能是考试吧。 Maybe this is the first thing you ask someone when you are hiring a MongoDB developer: how do you do timestamps?也许这是你在招聘 MongoDB 开发人员时问别人的第一件事:你如何做时间戳? Well, that's what I'd ask anyway because if you know you've probably learned most of the API by that stage.好吧,无论如何我都会问这个问题,因为如果您知道到那个阶段您可能已经了解了 API 的大部分内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM