【问题标题】:Running batch operations in MongoDB/ Robo3t在 MongoDB/Robo3t 中运行批处理操作
【发布时间】:2021-03-04 15:43:55
【问题描述】:

我有一个文件中的用户列表,我想更新他们在集合中的记录。

db.getCollection('users').update({username: "<a user>"}, { $set: { <set some values here> }})

如何将用户列表输入此命令或 Robo 3T 中的类似命令或从终端命令行输入?

【问题讨论】:

标签: mongodb robo3t


【解决方案1】:

命令行中的以下选项似乎更简单:

选项 A) 从用户列表中即时生成更新查询并发送到 mongo shell:

cat file.csv | awk '{ print("db.users.update({user:\""$1"\"},{ $set:{x:1} }) ")   }' | mongo 

选项 B) mongoimport

Step 1) 将用户列表导入到临时集合中的数据库中:

 mongoimport --type csv -d test -c usersToUpdate --headerline  file.csv

文件.csv:

 userlist
 John
 Donald
 Jeny

步骤 2) 导入集合后,您可以执行以下操作:

  db.usersToUpdate.find({},{_id:0,userlist:1}).forEach(function(theuser){      db.users.update({username: theuser.userlist}, { $set: { <set some values here> }}); print(theuser+" record updated successfully");      })

第 3 步)最后您可以使用以下命令清理临时 usersToUpdate 集合:

  db.usersToUpdate.drop() 

【讨论】:

    猜你喜欢
    • 2011-06-04
    • 2023-01-25
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多