2016-10-24 3 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?" 
     } 
    ] 
    }] 

Я хочу извлечь только заголовок и идентификатор обоих рецептов при доступе к данному маршруту. В настоящее время я использую эту функцию, которая возвращает все поля обоих рецептов.

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

Какой запрос следует использовать для извлечения только названия и идентификатора двух рецептов, а не всех деталей?

ответ

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

Хотя у меня есть еще один вопрос, как ddo –

+0

Можете ли вы обновить свой вопрос? – mani