我試圖過濾返回的數據集與流星的find().fetch()
只包含一個對象,它不顯示非常有用,如果我查詢單個子文檔,而是我接收幾個,有些甚至不包含任何匹配的術語。流星:只返回集合中的嵌套數組中的單個對象
我有一個簡單的混合數據收集,看起來像這樣:
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e7"),
"name" : "Entertainment",
"items" : [
{
"_id" : ObjectId("57a38b5f2bd9ac8225caff06"),
"slug" : "this-is-a-long-slug",
"title" : "This is a title"
},
{
"_id" : ObjectId("57a38b835ac9e2efc0fa09c6"),
"slug" : "mc",
"title" : "Technology"
}
]
}
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e8"),
"name" : "Sitewide",
"items" : [
{
"_id" : ObjectId("57a38bc75ac9e2efc0fa09c9"),
"slug" : "example",
"name" : "Single Example"
}
]
}
我可以很容易地嵌套items
陣列與MongoDB的殼,因爲這在查詢特定對象:
db.categories.find({ "items.slug": "mc" }, { "items.$": 1 });
這將返回良好的數據,它只包含我想要使用的單個對象:
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e7"),
"items" : [
{
"_id" : ObjectId("57a38b985ac9e2efc0fa09c8")
"slug" : "mc",
"name" : "Single Example"
}
]
}
但是,如果內流星類似的查詢被直接嘗試:
/* server/publications.js */
Meteor.publish('categories.all', function() {
return Categories.find({}, { sort: { position: 1 } });
});
/* imports/ui/page.js */
Template.page.onCreated(function() {
this.subscribe('categories.all');
});
Template.page.helpers({
items: function() {
var item = Categories.find(
{ "items.slug": "mc" },
{ "items.$": 1 })
.fetch();
console.log('item: %o', item);
}
});
的結果是不理想的,因爲它返回整個匹配塊,以及嵌套items
陣列中的每個對象:
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e7"),
"name" : "Entertainment",
"boards" : [
{
"_id" : ObjectId("57a38b5f2bd9ac8225caff06")
"slug" : "this-is-a-long-slug",
"name" : "This is a title"
},
{
"_id" : ObjectId("57a38b835ac9e2efc0fa09c6")
"slug" : "mc",
"name" : "Technology"
}
]
}
我當然可以用一個for循環來進一步篩選返回的遊標,以獲得所需的對象,但是在處理較大的數據集時,這看起來不可取,效率極低。
我不明白爲什麼流星的find
返回的數據集完全不同於MongoDB的shell find
,唯一合理的解釋是兩個函數簽名都不一樣。
我是否應該將嵌套集合分解爲更小的集合並採用更多關係數據庫方法(即存儲對ObjectID的引用)和查詢集合之間的數據,還是有更強大的方法來有效地過濾大型數據集合成只包含匹配對象的單個對象,如上所示?
流星的客戶端實現被稱爲MiniMongo。它只實現MongoDB選擇器的一個子集。 – MasterAM