简体   繁体   English

如何在 mongoimport 中使用 mongodb 函数?

[英]How to use mongodb functions with mongoimport?

Let's say I want to insert an object that contains date objects using mongoimport from the commandline.假设我想从命令行使用 mongoimport 插入一个包含日期对象的 object。

echo "{\"int_key\": 1, \"date_key\": new Date(\"2022-12-27\")}" | mongoimport --host "192.168.60.10" --db example_db --collection example_collection

will not work because the object I am trying to insert is not in the form of a valid JSON. The reason I want to use mongoimport is because there is an array of a large number of objects that I want to persist at one go. If I try to use the mongo command the argument length for --eval is too long.将不起作用,因为我尝试插入的 object 不是有效的 JSON 的形式。我想使用 mongoimport 的原因是因为有大量对象的数组,我想在一个 go 中保留。如果我尝试使用mongo命令--eval的参数长度太长。 For example,例如,

mongo --host "192.168.60.10" --eval "db=db.getSiblingDB(\"example_db\");db.getCollection(\"example_collection\").insert([{\"int_key\": 1, \"date_key\": new Date(\"2022-12-27\")}])"

but the array inside insert() has a very large number of objects.但是insert()中的数组有非常多的对象。 Can you suggest any workaround to this?你能建议任何解决方法吗? I was thinking I could use mongoimport to read all the objects put into an array through stdin or a file.我在想我可以使用mongoimport来读取通过标准输入或文件放入数组中的所有对象。 The options for using a json array would not allow the kind of array of objects I insert using the insert() in mongo --eval .使用 json 数组的选项不允许我使用mongo --eval中的insert()插入的对象数组类型。

You have to use this你必须使用这个

echo "{\"int_key\": 1, \"date_key\": {\"$date\": \"2022-12-27\"}}"

It may require:它可能需要:

echo "{\"int_key\": 1, \"date_key\": {\"$date\": \"2022-12-27T00:00:00Z\"}}"

For other data types see MongoDB Extended JSON (v2)对于其他数据类型,请参见MongoDB 扩展 JSON (v2)

I use mongoimport in the same way to insert around 6 billion documents per day, it is very fast and reliable.我以同样的方式使用mongoimport每天插入大约 60 亿个文档,它非常快速和可靠。

Depending on how you use it, mongoimport does not import small amount of documents could be relevant for you.根据您的使用方式, mongoimport 不会导入少量可能与您相关的文档

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

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