itsource

MongoDB 컬렉션 하이픈으로 연결된 이름

mycopycode 2023. 3. 13. 20:28
반응형

MongoDB 컬렉션 하이픈으로 연결된 이름

MongoDB 데이터베이스에 데이터를 삽입하기 위해 Node.js 프로그램을 사용하고 있습니다."repl-failOver"라는 이름의 컬렉션에 데이터를 삽입했습니다.

var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://localhost:30002/test", function(err, db) {
    if (err) throw err;
    db.collection("repl-failOver").insert( { "documentNumber" : document++}, function (err, doc) {
        if (err) throw err;
        console.log(doc);
    });
    db.close();
});

Mongo 쉘을 사용하여 데이터베이스 내의 컬렉션을 나열할 때show collections컬렉션 "repl-failOver"가 표시됩니다.

이 컬렉션에 대해 mongo 셸에서 find 명령을 실행하려면 어떻게 해야 합니까?

다음 구문을 사용합니다.

db['repl-failOver'].find({})

또는

db.getCollection('repl-failOver').find({})

자세한 내용은 매뉴얼의 쿼리 실행 섹션에서 확인할 수 있습니다.

이름에 공백, 하이픈 또는 숫자로 시작하는 등 mongo 셸이 컬렉션 이름을 받아들이지 않는 경우 다음과 같이 대체 구문을 사용하여 컬렉션을 참조할 수 있습니다.

db["3test"].find()

db.getCollection("3test").find()

이 에러는, 특정의 문자의 컬렉션에 액세스 했을 때에 표시됩니다( ).-,_,여기서 회피책을 설명했습니다만, 기본적으로 필요한 것은,

db.getCollection("repl-failOver").insert(...)

언급URL : https://stackoverflow.com/questions/24711939/mongodb-collection-hyphenated-name

반응형