복붙노트

[MONGODB] BATCHSIZE 필드 이름은 필드 프로젝션 무시

MONGODB

BATCHSIZE 필드 이름은 필드 프로젝션 무시

나는 user_batch의 컬렉션이 있습니다. 그것은 다음과 같은 문서가 포함되어 있습니다 :

[{
  _id: ObjectId("594baf96256597ec035df23c"),
  name: "Batch 1",
  batchSize: 30,
  users:[]
 },
 {
  _id: ObjectId("594baf96256597ec035df234"),
  name: "Batch 2",
  batchSize: 50,
  users:[]
 }]

찾기 쿼리에서 난 단지 이름과 BATCHSIZE을 투사합니다. 내가 nodejs에서 찾기 쿼리를 실행할 때, 나는 쿼리 결과에서 전체 문서를 얻고있다. 질문:

db.collection('user_batch').find({}, {name: 1, batchSize: 1}).toArray((err, result) => {
  if(err) 
    console.log(err)
  else
    console.log(result)
})

난 그냥 {이름 : 1} 통과하면 그것은 _id 프로젝트와 이름이됩니다. 내가 BATCHSIZE를 통과한다면 그것은 전체 문서를 반환합니다.

참고 : 몽고 셸에서이 쿼리를 실행하는 동안 나는이 문제에 직면하고 있지 않다

해결법

  1. ==============================

    1.당신은 드라이버가 잘못 BATCHSIZE 옵션으로 해석하고 투사 문을 무시 정확합니다.

    당신은 드라이버가 잘못 BATCHSIZE 옵션으로 해석하고 투사 문을 무시 정확합니다.

    현대 드라이버 릴리스에서이 생각을 할 수있는 올바른 방법은 실제로의 .project () "커서 방법"을 대신 사용하는 것입니다. 이것은 다른 언어 드라이버 구현과 더 일치한다.

        db.collection('collection').find()
          .project({ name: 1, batchSize: 1})
          .toArray();
    

    전체 데모로 :

    const mongodb = require('mongodb'),
          MongoClient = mongodb.MongoClient;
    
    
    (async function() {
    
      let db;
    
      try {
        db = await MongoClient.connect('mongodb://localhost/test');
    
        // New form uses .project() as a cursor method
        let result = await db.collection('collection').find()
          .project({ name: 1, batchSize: 1})
          .toArray();
    
        console.log(JSON.stringify(result,undefined,2));
    
        // Legacy form confuses this as being a legacy "cursor option"
        let other = await db.collection('collection')
          .find({},{ name: 1, batchSize: 1 })
          .toArray();
    
        console.log(JSON.stringify(other,undefined,2));
    
      } catch(e) {
        console.error(e)
      } finally {
        db.close()
      }
    
    })()
    

    출력을 생성합니다 :

    [
      {
        "_id": "594baf96256597ec035df23c",
        "name": "Batch 1",
        "batchSize": 30
      },
      {
        "_id": "594baf96256597ec035df234",
        "name": "Batch 2",
        "batchSize": 50
      }
    ]
    [
      {
        "_id": "594baf96256597ec035df23c",
        "name": "Batch 1",
        "batchSize": 30,
        "users": []
      },
      {
        "_id": "594baf96256597ec035df234",
        "name": "Batch 2",
        "batchSize": 50,
        "users": []
      }
    ]
    

    제 1 출력 형태) ((가)의 .project를 사용하여 보정 한 어디에

  2. ==============================

    2.찾기의 구문이 변경되었습니다. 아래는 내가이 문제를 해결하기 위해 알 필요가 것입니다. 이것은 https://github.com/mongodb/node-mongodb-native/blob/master/CHANGES_3.0.0.md#find에서 발췌

    찾기의 구문이 변경되었습니다. 아래는 내가이 문제를 해결하기 위해 알 필요가 것입니다. 이것은 https://github.com/mongodb/node-mongodb-native/blob/master/CHANGES_3.0.0.md#find에서 발췌

    찾아 findOne 더 이상 필드 매개 변수를 지원하지 않습니다. 당신은 동일한 결과를 얻을 수 있습니다 Cursor.prototype.project 사용하거나 투영 속성 전달하여 필드 파라미터 옵션 객체에있다. 또한, 발견은 건너 뛰기와 같은 개별 옵션을 지원하지 않습니다 위치 파라미터로 제한한다. 당신도, 옵션 객체에서 이러한 매개 변수에 전달해야합니다 또는 Cursor.prototype.skip 같은 커서 방법을 통해 추가합니다.

    2.X 구문 :

    const cursor = coll.find({ a: 42 }, { someField: 1 });
    

    3.X 구문 :

    const cursor = coll.find({ a: 42 }).project({ someField: 1 });
    
    /* OR */
    
    const cursor = coll.find({ a: 42 }, { projection: { someField: 1 } });
    
  3. from https://stackoverflow.com/questions/45098742/batchsize-field-name-ignored-in-field-projection by cc-by-sa and MIT license