如何创建视图';mongoDb在流星中的应用
我是meteor的新手,我的要求是创建视图,以便在mongo中创建一个由多个家长共享的文档。因此,如何创建一个视图,使两个或多个父级在逻辑上而不是物理上共享同一文档如何创建视图';mongoDb在流星中的应用,mongodb,meteor,meteor-blaze,Mongodb,Meteor,Meteor Blaze,我是meteor的新手,我的要求是创建视图,以便在mongo中创建一个由多个家长共享的文档。因此,如何创建一个视图,使两个或多个父级在逻辑上而不是物理上共享同一文档 提前感谢您的帮助Meteor在数据库级别上没有视图概念 您可以在mongo collection中定义数据,并使用发布将其提供给客户端 默认情况下,发布返回来自一个集合。 如果要基于集合之间的“联接”返回数据,可以使用此软件包:这样,当集合中的数据发生更改时,整个发布将重新计算,并且您将拥有新版本的数据 Meteor在数据库级别上没
提前感谢您的帮助Meteor在数据库级别上没有视图概念 您可以在mongo collection中定义数据,并使用发布将其提供给客户端 默认情况下,发布返回来自一个集合。
如果要基于集合之间的“联接”返回数据,可以使用此软件包:这样,当集合中的数据发生更改时,整个发布将重新计算,并且您将拥有新版本的数据 Meteor在数据库级别上没有视图概念 您可以在mongo collection中定义数据,并使用发布将其提供给客户端 默认情况下,发布返回来自一个集合。
如果要基于集合之间的“联接”返回数据,可以使用此软件包:这样,当集合中的数据发生更改时,整个发布将重新计算,并且您将拥有新版本的数据 您可以在Meteor中创建Mongo视图,如下所示:
// Use a collection that already exists that you're already using
const SomeCollection = new Mongo.Collection('some_collection');
// Then grab the raw db handler
// @link http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html
const db = SomeCollection.rawDatabase();
// Unfortunately, this driver doesn't support the `db.createView` method
// So let's use `db.createCollection` instead
// @link https://docs.mongodb.com/manual/reference/method/db.createCollection/#db.createCollection
db.createCollection('orderView', { // name of the view to create
viewOn: 'order', // name of source collection from which to create the view
pipeline: [{
$lookup: {
from: 'customer',
localField: 'customerId',
foreignField: '_id',
as: 'customerDoc',
} }, { $unwind: '$customerDoc' }
],
}).then((view) => {
// do stuff with your view
});
db
操作是通过承诺来处理的,因此您可能希望使用async
和wait
来保持整洁
const createView = async (viewName, viewOn, pipeline) =>
await db.createCollection(viewName, { viewOn, pipeline });
// Someplace else in your code
const someMethod = async () => {
// ...
const myView = await createView('orderView', 'order', [{
$lookup: {
from: 'customer',
localField: 'customerId',
foreignField: '_id',
as: 'customerDoc',
} }, { $unwind: '$customerDoc' }
]);
// ...
};
您可以在Meteor中创建Mongo视图,如下所示:
// Use a collection that already exists that you're already using
const SomeCollection = new Mongo.Collection('some_collection');
// Then grab the raw db handler
// @link http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html
const db = SomeCollection.rawDatabase();
// Unfortunately, this driver doesn't support the `db.createView` method
// So let's use `db.createCollection` instead
// @link https://docs.mongodb.com/manual/reference/method/db.createCollection/#db.createCollection
db.createCollection('orderView', { // name of the view to create
viewOn: 'order', // name of source collection from which to create the view
pipeline: [{
$lookup: {
from: 'customer',
localField: 'customerId',
foreignField: '_id',
as: 'customerDoc',
} }, { $unwind: '$customerDoc' }
],
}).then((view) => {
// do stuff with your view
});
db
操作是通过承诺来处理的,因此您可能希望使用async
和wait
来保持整洁
const createView = async (viewName, viewOn, pipeline) =>
await db.createCollection(viewName, { viewOn, pipeline });
// Someplace else in your code
const someMethod = async () => {
// ...
const myView = await createView('orderView', 'order', [{
$lookup: {
from: 'customer',
localField: 'customerId',
foreignField: '_id',
as: 'customerDoc',
} }, { $unwind: '$customerDoc' }
]);
// ...
};
“流星”不是那样工作的。您将需要使用一个框架作为一个视图层,例如火焰层、反应层或角度层。要更改视图,请使用路由器。Meteor不是这样工作的。您将需要使用一个框架作为一个视图层,例如火焰层、反应层或角度层。要更改视图,请使用路由器。