mongodump:传递--query参数:解析命令行选项时出错

问题描述 投票:0回答:1

我有一个收藏sampleColl。

MongoDB Enterprise > db.sampleColl.find().limit(3);
{ "_id" : ObjectId("64f01e4f58e834c50cdf63c1"), "field" : "2ed2892b" }
{ "_id" : ObjectId("64f01e4f58e834c50cdf63c2"), "field" : "b48ec1ad" }
{ "_id" : ObjectId("64f01e4f58e834c50cdf63c3"), "field" : "1d02b0c2" }

当我尝试为整个集合创建 bson 文件时,它工作正常。

D:\mongodb-database-tools-100.8.0\bin>mongodump --uri="mongodb://127.0.0.1:27017/test" --collection=sampleColl --out=dump --gzip --verbose
2023-08-31T10:40:06.606+0530    dumping up to 1 collections in parallel
2023-08-31T10:40:06.613+0530    writing test.sampleColl to dump\test\sampleColl.bson.gz
2023-08-31T10:40:06.679+0530    done dumping test.sampleColl (10000 documents)

现在,如果我尝试使用

--query
参数限制文档,则会遇到错误。

D:\mongodb-database-tools-100.8.0\bin>mongodump --uri="mongodb://127.0.0.1:27017/test" --collection=sampleColl --query='{"field": {"$gt": "5"}}' --out=dump --gzip --verbose
2023-08-31T10:42:05.872+0530    error parsing command line options: error parsing positional arguments: provide only one MongoDB connection string. Connection strings must begin with mongodb:// or mongodb+srv:// schemes
2023-08-31T10:42:05.873+0530    try 'mongodump --help' for more information
mongodb command-line command-prompt database-backups mongodump
1个回答
0
投票

经过多次尝试和错误,

在 Windows 命令提示符中,使用

"
转义
\
,并将查询括在
"
中(双引号)。

mongodump --uri="mongodb://127.0.0.1:27017/test" --collection=sampleColl --query="{\"field\": {\"$gt\": \"5\"}}" --out=dump --gzip --verbose

在 Windows PowerShell (PSVersion 5.1.22000.2003) 中,使用

"
转义
\
,并将查询括在
'
中(单引号)。

mongodump --uri="mongodb://127.0.0.1:27017/test" --collection=sampleColl --query='{\"field\": {\"$gt\": \"5\"}}' --out=dump --gzip --verbose
© www.soinside.com 2019 - 2024. All rights reserved.