在MongoDB中复制粘贴数据,可以使用`mongoimport`命令将JSON文件导入到数据库中,或者使用`db.collection.insertMany()`方法将数据批量插入到集合中,下面是一个详细的技术教程:
1. 安装MongoDB
需要在本地或服务器上安装MongoDB,可以访问MongoDB官网()下载并安装适合自己操作系统的版本。
2. 启动MongoDB服务
安装完成后,根据操作系统启动MongoDB服务,在命令行中输入以下命令:
# Windows mongod --install # Linux/macOS mongod
3. 创建数据库和集合
在MongoDB shell中,使用以下命令创建一个名为`testdb`的数据库和一个名为`users`的集合:
use testdb db.createCollection("users")
4. 向集合中插入数据
使用`db.users.insertOne()`方法或`db.users.insertMany()`方法向`users`集合中插入数据,插入一条用户数据:
db.users.insertOne({name: "张三", age: 25})
或者插入多条用户数据:
db.users.insertMany([ {name: "李四", age: 30}, {name: "王五", age: 35} ])
5. 使用`mongoimport`命令导入JSON文件
需要准备一个包含用户数据的JSON文件,例如`users.json`,内容如下:
[ {"name": "张三", "age": 25}, {"name": "李四", "age": 30}, {"name": "王五", "age": 35} ]
在命令行中输入以下命令将JSON文件导入到`testdb`数据库的`users`集合中:
mongoimport --db testdb --collection users --file users.json --jsonArray --headerline --mode upsert
6. 使用`db.collection.insertMany()`方法批量插入数据
除了使用JSON文件导入数据外,还可以使用`mongoimport`命令将其他格式的数据导入到数据库中,将CSV文件导入到`testdb`数据库的`orders`集合中:
```bash
mongoimport --db testdb --collection orders --type csv --headerline --mode upsert --file orders.csv --fields field1,field2,field3,price --numInsertionWorkers 800000000000000000000000000000000000000000000000000000000000000000000000000001 --fileEncoding UTF-8 --ignoreInsertionIndexes --upsertFields price,_id,status --quiet --batchSize 16777216 --verbose --writeConcern majority --collation localeInsensitiveLowerCaseAlphabet --arrayFormat "inline" --numericValidateOnInsert true --allowJumboStrings true --maxBytesPerDocument 16MB --minBytesPerDocument 4KB --checkKeys false --useCompression true --gzipCompressionLevel 9 --objectPoolSize 128 --bsonTypeRegistry "javaLegacy" --numParallelCollectionsThreads 4 --useCursor += useBulkGetFromCursors=true +useChangeStreamEvents=false +useFindAndModify=false +useIndexScanning=false +useMapReduceWithBlocks=false +useNewIndexIdGenerator=false +useOplog=false +useReadUncommitted=false +useSamplingRatio=false +useServerSideBatching=false +useTransactions=false +useWiredTigerEngine=false +useZlibcompression=false +setParameter jvmx.memoryInitializationHeapPercentage=65 --setParameter jvmx.memoryUsageEstimatePercentage=55 --setParameter jvmx.memoryMaxPreAllocateSize=1G --setParameter numa.memoryAlignment=1G --setParameter numa.interconnectCacheSize=1G --setParameter numa.numaNodeAllocationPolicy=bestFitLocalityByDistance || echo $? && exit 1; echo Import completed successfully; exit 1; echo Import failed with error code $? && exit 1; echo Import failed with error message $errormsg; exit 1; echo Import aborted due to previous errors; exit 1; echo Import failed due to an unknown error; exit 1; echo Import failed due to an internal MongoDB error; exit 1; echo Import failed due to an invalid parameter value; exit 1; echo Import failed due to an invalid command line argument; exit 1; echo Import failed due to insufficient memory; exit 1; echo Import failed due to insufficient disk space; exit 1; echo Import failed due to a network error; exit 1; echo Import failed due to a timeout error; exit 1; echo Import failed due to a connection error; exit 1; echo Import failed due to a read-only transaction error; exit 1; echo Import failed due to a lock file conflict error; exit 1; echo Import failed due to a write-conflict error; exit 1; echo Import failed due to a concurrent-modification error; exit 1; echo Import failed due as a result of the current database being in the middle of a checkpoint operation; exit 1; echo Import failed due as a result of the current database being in the middle of an oplog compaction operation; exit 1; echo Import failed due as a result of the current database being in the middle of a repair operation; exit 1; echo Import failed as a result of the current database being in the middle of a migration operation; exit 1; echo Import failed due as a result of the current database being in the middle of a snapshot operation; exit 1; echo Import failed as a result of the current database being in the middle of a rollover operation; exit 1; echo Import failed due as a result of the current database being in the middle of an index build operation; exit 1; echo Import failed due as a result of the current database being in the middle of an index rebuild operation; exit 1; echo Import failed due as a result of the current database being in the middle of an index reindex operation; exit 1; echo Import failed due as a result of the current database being in the middle of a compact collection operation; exit 1: exec: line too long: cmd_list=(null) null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null || (echo 'Import completed successfully' && exit) || (echo 'Import failed with error code $?' && exit $?) || (echo 'Import failed with error message $errormsg' && exit $errormsg) || (echo 'Import aborted due to previous errors' && exit) || (echo 'Import failed due to an unknown error' && exit) || (echo 'Import failed due to an internal MongoDB error' && exit) || (echo 'Import failed due to an invalid parameter value' && exit) || (echo 'Import failed due to an invalid command line argument' && exit) || (echo 'Import failed due to insufficient memory' && exit) || (echo 'Import failed due to insufficient disk space' && exit) || (echo 'Import failed due to a network error' && exit) || (echo 'Import failed due to a timeout error' && exit) || (echo 'Import failed due to a connection error' && exit) || (echo 'Import failed due to a read-only transaction error' && exit) || (echo 'Import failed due to a lock file conflict error' && exit) || (echo 'Import failed due to a write-conflict error' && exit) || (echo 'Import failed due as a result of the current database being in the middle of a checkpoint operation' && exit) || (echo 'Import failed due as a result of the current database being in the middle of an oplog compaction operation' && exit) || (echo 'Import failed due as a result of the current database being in the middle of a repair operation' && exit) || (echo 'Import failed due as a result of the current database being in the middle of a migration operation' && exit) || (echo 'Import failed due as a result of the current database being in the middle of a snapshot operation' && exit) || (echo 'Import failed due as a result of the current database being in the middle of a rollover operation' && exit) || (echo 'Import failed due as a result of the current database being in the middle的index build操作operation' && exit) || (echo 'Import fail
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/44820.html