MeteorJS-将图像(FS.collection)链接到其MongoDB Collection中的相关文档 [英] MeteorJS - Linking images (FS.collection) to their relevant document in the MongoDB Collection
问题描述
我正在用Meteorjs构建一个应用程序.我有一个初始化为的集合:
I'm building an app with Meteorjs. I have a collection that is initialized as:
Places=new Mongo.Collection('places');
我还想在其中存储相应位置的图像.我已将collectionFS软件包添加到我的项目中.以下代码为图像创建一个单独的集合:
in which I also want to store the images of the corresponding place. I've added the collectionFS package to my project. The following code creates a separate collection for the images:
image=new FS.Collection("images", {
stores: [new FS.Store.FileSystem("images")]
});
但是,我不明白如何将这组图像与其在位置"中的相关文档相关联.或者只是在我的mongo收藏夹中插入一张图片.
However, I fail to understand how am I supposed to associate this set of images to its relevant document in 'Places'. Or simply insert an image in my mongo collection.
推荐答案
在Meteor/Mongo中这是常见的情况,您想要关联两个集合. mongo文档在这方面写得很好.
This is a common case in Meteor/Mongo where you want to relate two collections. The mongo docs have a good writeup on this.
比方说每个地方可以有很多图像.您可以在图像中添加对该位置的引用,也可以引用该位置中的许多图像.
Let's say each place can have many images. You can either put a reference to the place inside the image or refer to the many images from the place.
在collectionFS中创建图像时(不包括细节),请确保保留图像的_id
:
When you create an image in collectionFS (leaving out the specifics), make sure to keep the _id
of the image:
imgId = image.insert();
如果要让图像引用该位置,则可以使用以下方法更新图像:
If you want to have the image refer to the place you can then update the image with:
image.update({ _id: imgId },{ $set: { placeId: myPlace._id }});
或者您可以$push
imgId
到您所在位置的图像阵列上:
or you can $push
imgId
onto an array of images inside your place:
Places.update({ _id: myPlace._id },{ $push: { imageArray: imgId }});
第二种形式的引用更加灵活,因为同一图像可以属于多个位置(多对多).这适合嵌套的地方,例如 Times Square 的图片既是 Times Square 的图片,又是 New York City 的图片,等等.
The second form of reference is a bit more flexible in that the same image can belong to multiple places (many-to-many). This is good for nested places, for example a picture of Times Square is both a picture of Times Square and a picture of New York City and so on.
您可以使用 reywood:publish-composite 软件包,旨在轻松发布相关馆藏.
Either way you can join your image
and Places
collections using the reywood:publish-composite package which is designed for easy publishing of related collections.
还要注意,在流星中命名收藏的常见约定是首字母大写和复数形式.即图片而不是 image .这是因为集合是Meteor中的全局变量,并且集合包含许多以它命名的东西.
Note also that a common convention for naming collections in Meteor is first letter capitalized and plural form. i.e. Images instead of image. This is because collections are global variables in Meteor and a collection holds many of the thing that it's named after.
这篇关于MeteorJS-将图像(FS.collection)链接到其MongoDB Collection中的相关文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!