字符串到ISO日期

时间:2018-03-23 10:35:21

标签: mongodb

我已经看了好几次,到目前为止还没有答案。我想将字符串日期转换为ISODate - 我不希望它转换为UTC或来自UTC,我只想将存储为字符串的日期值存储作为日期。

我不想要UTC,因为我想根据世界任何地方的时间(例如:12pm)查询和计算,我按小时,天,周和月进行求和,并比较多个数据国家根据当地时间。

目前我有一个日期作为字符串,我想用新名称添加到新文档中:

“transaction_date”:“$ object_raw_origin_data.register_sales.sale_date”

以下字符串的日期值显示为:

“sale_date”:“2018-03-13 20:05:46”

我希望它是:“transaction_date”:ISODate(“2018-03-13 20:05:46”)

日期和时间必须读取2018-03-13 20:05:46未转换为UTC。

谢谢,马特

原始数据JSON

{ 
"object_category" : "application", 
"object_type" : "register-sales-24-months", 
"object_origin" : "vend", 
"tenant_uuid" : "00000000-0000-0009-9999-999999999999", 
"party_uuid" : "8d519765-05d2-469f-ad35-d7a22fa9df2f", 
"subscription_uuid" : "0", 
"connection_uuid" : "6ed9bd79-d9c5-4296-a821-7e15b1c69e6c", 
"status" : "", 
"object_created" : ISODate("2018-03-15T21:40:57.158+0000"), 
"object_raw_origin_data" : {
    "pagination" : {
        "results" : NumberInt(75964), 
        "page" : NumberInt(1), 
        "page_size" : NumberInt(200), 
        "pages" : NumberInt(380)
    }, 
    "register_sales" : {
        "id" : "776a66f2-993c-b372-11e8-26f9d3c1bdde", 
        "source" : "USER", 
        "source_id" : "", 
        "register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c", 
        "market_id" : "3", 
        "customer_id" : "02d59481-b67d-11e5-f667-b08185e8f6d5", 
        "customer_name" : "", 
        "customer" : {
            "id" : "02d59481-b67d-11e5-f667-b08185e8f6d5", 
            "name" : "", 
            "customer_code" : "WALKIN", 
            "customer_group_id" : "02d59481-b67d-11e5-f667-b08185e893f8", 
            "customer_group_name" : "All Customers", 
            "updated_at" : "2016-01-01 12:16:44", 
            "deleted_at" : "", 
            "balance" : "0", 
            "year_to_date" : "0", 
            "date_of_birth" : "", 
            "sex" : "", 
            "custom_field_1" : "", 
            "custom_field_2" : "", 
            "custom_field_3" : "", 
            "custom_field_4" : "", 
            "note" : "", 
            "contact" : {
            }
        }, 
        "user_id" : "02d59481-b655-11e5-f667-dca974edc4ea", 
        "user_name" : "Alvaro Velosa", 
        "sale_date" : "2018-03-13 20:05:46", 
        "created_at" : "2018-03-13 20:06:00", 
        "updated_at" : "2018-03-13 20:06:00", 
        "total_price" : 4.5, 
        "total_cost" : 3.34, 
        "total_tax" : NumberInt(0), 
        "tax_name" : "No Tax", 
        "note" : "", 
        "status" : "CLOSED", 
        "short_code" : "wqgsgi", 
        "invoice_number" : "Masonic2108Temple", 
        "accounts_transaction_id" : "", 
        "return_for" : "", 
        "register_sale_products" : [
            {
                "id" : "776a66f2-993c-b372-11e8-26f9e92cb9bc", 
                "product_id" : "02dcd191-ae55-11e7-f130-9d4f4bcd91b1", 
                "register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c", 
                "sequence" : "0", 
                "handle" : "LAGERDRAUGHT300", 
                "sku" : "10287", 
                "name" : "LAGER DRAUGHT £3.00", 
                "quantity" : NumberInt(1), 
                "price" : 3.5, 
                "cost" : 2.74, 
                "price_set" : NumberInt(0), 
                "discount" : NumberInt(0), 
                "loyalty_value" : NumberInt(0), 
                "tax" : NumberInt(0), 
                "tax_id" : "02d59481-b67d-11e5-f667-b08185ec2871", 
                "tax_name" : "No Tax", 
                "tax_rate" : NumberInt(0), 
                "tax_total" : NumberInt(0), 
                "price_total" : 3.5, 
                "display_retail_price_tax_inclusive" : "0", 
                "status" : "CONFIRMED", 
                "attributes" : [
                    {
                        "name" : "line_note", 
                        "value" : ""
                    }
                ]
            }, 
            {
                "id" : "776a66f2-993c-b372-11e8-26f9e98104e0", 
                "product_id" : "02dcd191-ae55-11e7-f130-9d50e948a0b5", 
                "register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c", 
                "sequence" : "0", 
                "handle" : "SOFTDRINK", 
                "sku" : "10292", 
                "name" : "SOFT DRINK", 
                "quantity" : NumberInt(1), 
                "price" : NumberInt(1), 
                "cost" : 0.6, 
                "price_set" : NumberInt(0), 
                "discount" : NumberInt(0), 
                "loyalty_value" : NumberInt(0), 
                "tax" : NumberInt(0), 
                "tax_id" : "02d59481-b67d-11e5-f667-b08185ec2871", 
                "tax_name" : "No Tax", 
                "tax_rate" : NumberInt(0), 
                "tax_total" : NumberInt(0), 
                "price_total" : NumberInt(1), 
                "display_retail_price_tax_inclusive" : "0", 
                "status" : "CONFIRMED", 
                "attributes" : [
                    {
                        "name" : "line_note", 
                        "value" : ""
                    }
                ]
            }
        ], 
        "totals" : {
            "total_tax" : NumberInt(0), 
            "total_price" : 4.5, 
            "total_payment" : 4.5, 
            "total_to_pay" : NumberInt(0)
        }, 
        "register_sale_payments" : [
            {
                "id" : "776a66f2-993c-b372-11e8-26f9eab2cb46", 
                "payment_type_id" : "1", 
                "register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c", 
                "retailer_payment_type_id" : "02d59481-b655-11e5-f667-b0a23bc0e7bc", 
                "name" : "Cash", 
                "label" : "Account Customer", 
                "payment_date" : "2018-03-13 20:05:46", 
                "amount" : NumberInt(10)
            }, 
            {
                "id" : "776a66f2-993c-b372-11e8-26f9eab6b21a", 
                "payment_type_id" : "1", 
                "register_id" : "02dcd191-ae55-11e6-edd8-ec8dce1d9e1c", 
                "retailer_payment_type_id" : "02d59481-b655-11e5-f667-b0a23bc0e7bc", 
                "name" : "Cash", 
                "label" : "Account Customer", 
                "payment_date" : "2018-03-13 20:05:46", 
                "amount" : -5.5
            }
        ]
    }
}
}

1 个答案:

答案 0 :(得分:0)

感谢@Saleem和@Krishna的帮助,但我尝试使用字符串中的$ conctr $ conctr来创建一个ISODate,但它还没有工作。

db.Vend_raw_transactions.aggregate(
[{
"$project": {
  "object_origin": "$object_origin",
  "company": "$party_uuid",
  "connection": "$connection_uuid",
  "object_creation_date": "$object_created",
//      "transaction_date": "$object_raw_origin_data.register_sales.sale_date",


  "transaction_date": {
     "$dateFromString":     {
        "dateString": {
        "$concat":  [ 
            "$substr": [ "$object_raw_origin_data.register_sales.sale_date", 0, 10 ], 
            "T",
            "$substr": [ "$object_raw_origin_data.register_sales.sale_date", 11, 9]
                ] 
            }
     }
  },


  "transaction_gross_value": {
    "$add": [
      "$object_raw_origin_data.register_sales.total_price",
      "$object_raw_origin_data.register_sales.total_tax"
    ]
  },
  "transaction_net_value": "$object_raw_origin_data.register_sales.total_price",
  "transaction_tax_value": "$object_raw_origin_data.register_sales.total_tax",
  "transaction_cost_value": "$object_raw_origin_data.register_sales.total_cost",
  "object_class": "goods-service-transaction",
  "object_origin_category": "point-of-sale",
  "object_type": "receipt",
  "object_origin_type": "offline",
  "transaction_reference": "$object_raw_origin_data.register_sales.invoice_number",
  "transaction_status": "$object_raw_origin_data.register_sales.status",
  "transaction_currency": "GBP",
  "party_name": "$object_raw_origin_data.register_sales.customer.customer_name",
  "party_identifier": "$object_raw_origin_data.register_sales.customer.customer_code",
  "staff_identifier": "$object_raw_origin_data.register_sales.user_id",
  "staff_name": "$object_raw_origin_data.register_sales.user_name",
  "line_items" : {
      "$map": {
          "input": "$object_raw_origin_data.register_sales.register_sale_products",
          "as" : "product",
          "in": {"item_name": "$$product.name", 
                "item_system_id": "$$product.id",
                "item_identifier": "$$product.sku",
                "item_category" : "sales-revenue",
                "item_quantity" : "$$product.quantity",
                "item_net_unit_sale_value" : "$$product.price",
                "item_net_unit_discount_value" : "$$product.discount",
                "item_net_unit_member_value" : "$$product.loyalty_value",
                "item_net_unit_cost_value" : "$$product.cost",
                "item_unit_tax_value" : "$$product.tax",
                "item_price_list_reference" : "$$product.price_set",
                "item_total_sale_value" : "$$product.price_total",
                "item_total_tax_value" : "$$product.tax_total"
          }
      }
  }
}
}
//  ,{"$out": "9SP_Source" }
])

我无法理解为什么这不起作用,似乎应该 - 我根据现有的字符串值创建一个新的字符串并在中间添加一个T但它没有'识别$ DateFromString。感谢

错误消息

{     "消息" :"无法识别的表达式' $ dateFromString'",     "堆" :" MongoError:无法识别的表达式' $ dateFromString'" +               "在queryCallback(C:\ Users \ mattl \ AppData \ Local \ Programs \ nosqlbooster4mongo \ resources \ app.asar \ node_modules \ mongodb-core \ lib \ cursor.js:223:25)" +               "在C:\ Users \ mattl \ AppData \ Local \ Programs \ nosqlbooster4mongo \ resources \ app.asar \ node_modules \ mongodb-core \ lib \ connection \ pool.js:541:18" +               "在_combinedTickCallback(内部/进程/ next_tick.js:131:7)" +               " at process._tickCallback(internal / process / next_tick.js:180:9)",     "名称" :" MongoError",     " OK" :0,     " ERRMSG" :"无法识别的表达式' $ dateFromString'",     "代码" :168,     "代号" :" InvalidPipelineOperator" }

我设法使用以下脚本制作另一个字符串,但$ dateFromString无法识别

db.Vend_raw_transactions.aggregate(
[{
"$project": {
          "origin_date": "$object_raw_origin_data.register_sales.sale_date",
  "transaction_date": {
            "$substr": [ "$object_raw_origin_data.register_sales.sale_date", 0, 10 ]
  },
        "transaction_time": {
            "$substr": [ "$object_raw_origin_data.register_sales.sale_date", 11, 9 ]
  },
  "new_string_date": {

    "$concat" :[
      {"$substr": [ "$object_raw_origin_data.register_sales.sale_date", 0, 10 ]}, 
      "T", 
      {"$substr": [ "$object_raw_origin_data.register_sales.sale_date", 11, 9 ]}]

}
}
      }
])

由于