mongoDB cant导入CSV文件

时间:2018-06-18 04:20:23

标签: mongodb import mongoimport

我正在尝试按照以下命令将CSV文件导入MongoDB:

"Syntax Error: missing ; before statement (shell):1" 

但我收到以下错误消息

mongoimport

要解决此错误,我必须在mongoshell环境外使用terminal

所以我只需转动error inserting documents: not authorized on Northwind to execute command { insert: "employee-territories", writeConcern: { getLastError: 1, w: 1 }, ordered: false, $db: "Northwind" }

不幸的是我收到了错误消息

from extensions import bcrypt, db

class User(db.Model):
    # this config is used by sqlalchemy to store model data in the database
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(150))
    email = db.Column(db.String(100), unique=True)
    password = db.Column(db.String(100))

    def __init__(self, name, email, password, fav_movie):
        self.name = name
        self.email = email
        self.password = password

    @classmethod
    def seed(cls, fake):
        user = User(
            name = fake.name(),
            email = fake.email(),
            password = cls.encrypt_password(fake.password()),
        )
        user.save()

    @staticmethod
    def encrypt_password(password):
        return bcrypt.generate_password_hash(password).decode('utf-8')

    def save(self):
        db.session.add(self)
        db.session.commit()

我的代码出了什么问题?请帮忙

2 个答案:

答案 0 :(得分:1)

看起来,您忘记提供身份验证数据(用户名,密码和身份验证数据库)。

你的命令应该是这样的:

mongoimport -u "your username" -p "yoyr password" --authenticationDatabase "admin" -d Northwind -c "employee-territories" --type csv --file "/home/ubuntu/Downloads/northwind-mongo-master/employee-territories.csv" --headerline

查看mongoimport documentation

答案 1 :(得分:0)

感谢您的回复,但在添加the user namepassword后,出现了以下错误

" Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed "

要解决此错误,我添加了--host--authenticationDatabase参数,并且在我们连接到远程MongoDB时它可以正常工作

在您的代码中使用以下类似的格式:

 mongoimport --host 127.0.0.1:27017 -u "admin" -p "admin123" --authenticationDatabase admin  -d Northwind -c "employee-territories" --type csv --file "/home/ubuntu/Downloads/northwind-mongo-master/employee-territories.csv" --headerline