나는 간단한 포함 된 문서가 : 나는 몽고 쉘을 통해 작업 데이터를 쿼리 할 때몽구스 findOne 빈 배열 필드를 반환
{
"username":"user001",
"name":"John",
"tasks":[
{
"id":0,
"title":"Candy",
"description":"Lots of candy for you",
"category":"food",
"cost":2500,
"candyTypes":[
{"name":"gum", "type":"sweet", "price":"2"},
{"name":"chocolate", "type":"tasty", "price":"3"}
]
}
]
}
을, 내가 할 모든 :
db.users.findOne({ 'username': 'user001', 'tasks.id':4 }, {'tasks.$':1})
/* returns */
"tasks":[
{
"id":0,
"title":"Candy",
"description":"Lots of candy for you",
"category":"food",
"cost":2500,
"candyTypes":[
{"name":"gum", "type":"sweet", "price":"2"},
{"name":"chocolate", "type":"tasty", "price":"3"}
]
}
]
그러나 때 내가 몽구스에서 동일한 작업을 수행하려고하면 candyTypes
배열은 다시 빈 제공 :
Users.findOne({ 'username': username, 'tasks.id':taskId }, {'tasks.$':1}, function (err, data) {
console.log(data);
});
/* returns */
"tasks":[
{
"id":0,
"title":"Candy",
"description":"Lots of candy for you",
"category":"food",
"cost":2500,
"candyTypes":[]
}
]
나는 MongoDB를하고 아주 새로운 해요 몽구스.하지만 문서를 검색하고보고 난 후에, 나는 무엇이 실종되었는지 알 수 없다.
UPDATE
내가 몇 사용자가 요청, 그래서 여기 내 몽구스 스키마입니다 : 몽구스와
var UserSchema = new mongoose.Schema({
username:String,
name:String,
tasks:[{
id: Number,
title: String,
description:String,
category: String,
cost: Number,
candyTypes:[{
title:String,
type:String,
value:String
}]
}]
});
스키마 정의를 표시 할 수 있습니까? – Veeram
그냥 사용자 이름과 taskId를 콘솔로 만들려고합니다. mongo 쉘에 사용하는 것과 동일합니다 –
예, 동일합니다. 사실 나는이 db 안에 하나의 테스트 사용자 만 가지고있다. –