【发布时间】:2019-09-15 22:58:02
【问题描述】:
这是我的架构:
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") 这样插入,它可以工作!请帮助如何在每次插入时自动执行 create new Date(date) 或如何解决此问题!
【问题讨论】:
-
您用来生成您尝试导入的数据的代码是什么样的?
-
@MattOestreich,看起来像:
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 -
@MattOestreich,我也使用服务生成 csv/json 格式的随机日期,但它也不起作用.. 可能架构有问题?我做对了吗?