MongoDB 日期插入
MongoDB date insert
这是我的架构:
db.createCollection("user_clicks", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "session_id", "country", "browser", "url", "date"],
properties: {
session_id: {
bsonType: "string",
description: "must be a string and is required" },
country: {
bsonType: "string",
description: "country name and is required"},
browser: {
bsonType: "string",
description: "browser name and is required"},
url : {
bsonType: "string",
description: "user click url and is required"},
date: {
bsonType: "date",
description: "localdatetime and is required"}}}})
这是我用来生成数据的代码:
mgeneratejs `{
"session_id": "$oid",
"country": "$country",
"browser": {
"$choose": {
"from": [
"Firefox",
"Chrome",
"Safari",
"Explorer"
],
"weights": [
1,
2,
2,
1
]
}
},
"url": {
"$choose": {
"from": [
"google.com/images",
"facebook.com/profile1538713",
"soundcloud.com/playlist03",
"some-url.com/home",
"sinoptik.ua/kyiv"
],
"weights": [
1,
2,
2,
1,
3
]
}
},
"date": {
"$date": {
"min": "2016-08-01T23:59:59.999Z",
"max": "2016-10-01T23:59:59.999Z"
}
}
}` -n 5 | mongoimport --uri="mongodb://localhost:27017/events" --collection user_clicks --mode=insert
我正在尝试使用 mgeneratejs 和 mongoimport 生成随机日期。问题是我无法插入任何日期,例如:"3/13/2019" 或 "2019-03-26T23:44:26Z"(这就是我真正需要的)。错误是:
**WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})**
我尝试像 new Date("2019-03-26T23:44:26Z")
一样插入并且有效!请帮助如何在每次插入创建新日期(日期)时自动执行或如何解决此问题!
在插入随机日期之前使用下面的方法,它应该可以工作:
new Date(randomDate);
我对你的问题是什么感到困惑...一切正常..你将 Mongo
中的日期存储为 Date
对象(类型为 Date
)..
如果你想在 Mongo
中以类似 3/13/2019
的格式存储日期,你可以这样做:
// javascript
let dateToInsert = new Date("3/13/2019");
这是我的架构:
db.createCollection("user_clicks", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "session_id", "country", "browser", "url", "date"],
properties: {
session_id: {
bsonType: "string",
description: "must be a string and is required" },
country: {
bsonType: "string",
description: "country name and is required"},
browser: {
bsonType: "string",
description: "browser name and is required"},
url : {
bsonType: "string",
description: "user click url and is required"},
date: {
bsonType: "date",
description: "localdatetime and is required"}}}})
这是我用来生成数据的代码:
mgeneratejs `{
"session_id": "$oid",
"country": "$country",
"browser": {
"$choose": {
"from": [
"Firefox",
"Chrome",
"Safari",
"Explorer"
],
"weights": [
1,
2,
2,
1
]
}
},
"url": {
"$choose": {
"from": [
"google.com/images",
"facebook.com/profile1538713",
"soundcloud.com/playlist03",
"some-url.com/home",
"sinoptik.ua/kyiv"
],
"weights": [
1,
2,
2,
1,
3
]
}
},
"date": {
"$date": {
"min": "2016-08-01T23:59:59.999Z",
"max": "2016-10-01T23:59:59.999Z"
}
}
}` -n 5 | mongoimport --uri="mongodb://localhost:27017/events" --collection user_clicks --mode=insert
我正在尝试使用 mgeneratejs 和 mongoimport 生成随机日期。问题是我无法插入任何日期,例如:"3/13/2019" 或 "2019-03-26T23:44:26Z"(这就是我真正需要的)。错误是:
**WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})**
我尝试像 new Date("2019-03-26T23:44:26Z")
一样插入并且有效!请帮助如何在每次插入创建新日期(日期)时自动执行或如何解决此问题!
在插入随机日期之前使用下面的方法,它应该可以工作:
new Date(randomDate);
我对你的问题是什么感到困惑...一切正常..你将 Mongo
中的日期存储为 Date
对象(类型为 Date
)..
如果你想在 Mongo
中以类似 3/13/2019
的格式存储日期,你可以这样做:
// javascript
let dateToInsert = new Date("3/13/2019");