2016-10-24 2 views
0

mongoDB를 처음 사용하고 있으며 콜렉션의 객체에서 필드를 추출하는 방법을 알 수 없습니다. 다음과 같이 recipe라는 이름의 콜렉션이 있습니다 : -mongodb의 객체에서 일부 필드 추출하기

[ 
    { 
    "_id": "b44e864d-de8f-4110-b676-bbee13417b2e", 
    "title": "Fried Eggs", 
    "ingredients": [ 
     { 
     "name": "Eggs", 
     "amount": "10" 
     }, 
     { 
     "name": "Olive Oil", 
     "amount": "2 tbs" 
     } 
    ], 
    "steps": [ 
     "1. Heat the pan", 
     "2.Add Olive Oil", 
     "3. Add Eggs", 
     "4. Saute" 
    ], 
    "comments": [ 
     { 
     "_id": "8604e67c-2426-4742-bc91-25ee1c4064b5", 
     "poster": "Ron Swanson", 
     "comment": "Wrong instructions, Got stuck in Toaster" 
     }, 
     { 
     "_id": "040412bc-9046-49ca-9a65-19151b32cd91", 
     "poster": "Tom Haverford", 
     "comment": "What are Eggs?" 
     }, 
     { 
     "_id": "1034f082-b802-4382-be3e-ef50f07a530a", 
     "poster": "Andy Dwyer", 
     "comment": "What came firsst, Eggs or Chicken" 
     } 
    ] 
    }, 
    { 
    "_id": "da6f9798-6547-42f5-85ed-043efabeb196", 
    "title": "Boiled Eggs", 
    "ingredients": [ 
     { 
     "name": "Eggs", 
     "amount": "5" 
     }, 
     { 
     "name": "Water", 
     "amount": "3 Cups" 
     } 
    ], 
    "steps": [ 
     "1.Boil the Water", 
     "2.Add Eggs", 
     "3. Keepit for 5 mins", 
     "4. Let it Cool Down", 
     "5. Peel off the shell" 
    ], 
    "comments": [ 
     { 
     "_id": "cc569ebb-1307-499a-b9ee-7b24e557859f", 
     "poster": "Ron Swanson", 
     "comment": "Wrong instructions, Got stuck in Oven" 
     }, 
     { 
     "_id": "4f02756a-6e1b-4bda-a928-b3c5e03b55d8", 
     "poster": "Leslie Knope", 
     "comment": "What are Eggs?" 
     }, 
     { 
     "_id": "0f34a5cc-ebe3-41b5-8f0b-6d1a3c206e6b", 
     "poster": "Andy Dwyer", 
     "comment": "Can I remove the shell first and then boil?" 
     } 
    ] 
    }] 

주어진 경로에 액세스 할 때 두 요리법의 제목과 ID 만 추출하고 싶습니다. 현재이 두 함수의 모든 필드를 반환하는이 함수를 사용하고 있습니다.

var recipesCollection = db.collection("recipes"); 
     exports.getAllRecipes = function() { 
      return recipesCollection.find({}).toArray(); 
     }; 

두 가지 조리법의 제목과 ID 만 추출하려면 어떤 쿼리를 사용해야합니까?

답변

1

내 이해에 따라 특정 필드를 몽고 컬렉션에서 가져 오는 중입니다. 투영법을 사용하여이를 달성 할 수 있습니다.

var recipesCollection = db.collection("recipes"); 
exports.getAllRecipes = function() { 
    return recipesCollection.find({}, {"title": 1}).toArray(); 
}; 

자세한 내용은이 따르 https://docs.mongodb.com/v3.2/tutorial/project-fields-from-query-results/#return-the-specified-fields-and-the-id-field-only

+0

가 대단히 감사합니다. –

+0

질문이 하나 더 있는데, 어떻게해야합니까? –

+0

질문을 업데이트 할 수 있습니까? – mani

관련 문제