本文介绍了如何用Sails在Mongo中存储ObjectID的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个问题之后:处理Mongo和Sails中的关系吗?

After my initial question:Handling relationships in Mongo and Sails?

我设法使用了这种方法,但是现在我需要按照我最初的想法来解决这个问题.例如,我有照片"和类别"模型,集合,但现在我的类别还包含地址和商家详细信息.

I managed to use this approach, however I now need to resolve this the way I originally thought. For example, I have my Photo and Category models & collections, but now my category also contains addresses and business details.

在我的照片模型中,我有以下内容:

In my Photo model I have the following:

attributes: {
    caption  : { type  : 'string' }, 
    fid      : { type  : 'string' },
    user     : { model : 'user' },
    categories : { model : 'category', type : 'array'}
  }

在我的类别模型中,我有以下内容:

In my Category model I have the following:

  attributes: {
    photo       : { model: 'photo' },
    category    : { type: 'string'},
    address     : { type: 'string'},
    phone       : { type: 'integer'},

    //Foreign Keys
    categories   : { collection: 'photo', via: 'categories', type : 'integer'}
  }

现在,如果我删除类型数组并仅发送一个类别的ObjectID,则可以在Photo集合中将类别显示为ObjectID,但是对于我而言,一张照片可以具有多个类别.

Now I can get the categories in the Photo collection to show as an ObjectID if I removed the type array and just send a single ObjectID of a category, however in my case, a photo can have more than one Category.

如果我尝试在查询中发送ObjectID的数组,它们只会以String数组的形式显示在数据库中.无论如何,我可以将这些存储为ObjectID的数组吗?

If I try sending an array of ObjectID's in my query, they just show up in the database as a String array. Is there anyway I get store these as an array of ObjectID's instead?

推荐答案

基本上是many to many关系.您可以使用如下的水线方案:

Basically it's many to many relationship. You can use Waterline scheme like this:

照片模型

attributes: {
  caption  : { type  : 'string' }, 
  fid      : { type  : 'string' },
  user     : { model : 'user' },
  categories : { collection : 'category', via : 'photos'}
}

类别模型

attributes: {
  photos      : { collection: 'photo', via: 'categories' },
  category    : { type: 'string'},
  address     : { type: 'string'},
  phone       : { type: 'integer'},
}

这篇关于如何用Sails在Mongo中存储ObjectID的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:34