使用Meteor JS在MongoDB中创建两个集合的联接 [英] Create join of two collections in MongoDB using Meteor JS

查看:135
本文介绍了使用Meteor JS在MongoDB中创建两个集合的联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下数据结构:

收藏书:

{
  _id: "ajafsoaifj3341",
  title: "Hello World",
  poems: [
      {
        id: "12fjenofnoi23",
        addedAt: "2018-03-12...."
      },
      {
        id: "563jdfb34jfrr",
        addedAt: "2018-03-10...."
      },
      {
        id: "78a1ewqeqweq",
        addedAt: "2018-03-08...."
      }
   ]
}

珍藏诗:

{
  _id: "563jdfb34jfrr",
  title: "How to Join",
  author: "Smith"
  addedAt: "2017-12-21..."
}

我需要加入这两个收藏才能获得这首诗的标题.像这样:

I need to join these two collections to get the title of the poem. Something like:

{
  _id: "ajafsoaifj3341",
  title: "Hello World",
  poems: {
    id: "12fjenofnoi23",
    addedAt: "2018-03-12....",
    title: "How to Join"
  }
}

我喜欢这样的例子:

//SERVER
import { Poems } from '../imports/collections/poems';
import { Books } from '../imports/collections/books';

Meteor.publish('booksWithPoems', function (bookId) {
  const book = Books.findOne({ _id: bookId });
  return [
    Books.find({ _id: bookId }),
    Poems.find({
      _id: {
        $in: book.poems.id
      }
    })
  ];
});

//CLIENT
export default withTracker(props => {
const bookSubscription = Meteor.subscribe('booksWithPoems', props.editBookId);
return {
    editedBook: Books.findOne({}),
    editedPoems: Poems.find({}).fetch(),
    bookLoaded: bookSubscription.ready()
};
})(connect(mapStateToProps, {    
    ...
})(withRouter(BookEditor)));

但是this.props.editedPoems是未定义的.怎么了?

But this.props.editedPoems is undefined. What is wrong here?

也许这是更好的方法.如果您能在服务器和客户端代码方面为我提供帮助,我将不胜感激.我在流星/蒙哥世界中很陌生.

Maybe this are better ways to do this. I would appreciate if you could help me both with server and client code. I am quite new in Meteor/Mongo world.

最后一条评论.我不需要Poem文档中的所有数据,而只是"title".谢谢!

And the last comment. I do not need all data from Poem documents, just "title". Thanks!

推荐答案

您的出版物存在以下问题部分:$in期望使用数组,而book.poems.id是单首诗中的字符串值.但是,您想要的是book.poems中所有ID的数组.

Your publication has the following problematic part: $in excpects an array, while book.poems.id is a String value on a single poem. What you want, however is an array of all ids in book.poems.

这也是导致其他订阅(editedBook)接收某些数据而editedPoems没有接收到数据的原因.订阅仅反映那些在上次执行发布方法时发现的文档.

This is also the cause while the other subscription (editedBook) receives some data and editedPoems not. Subscription reflect only those documents, that have been found in the last execution of the publication method.

为了得到正确的答案,您需要先map诗歌ID,然后将此数组设置为$in的值:

In order to get this correct you need to first map Poems ids and set this array as value for $in:

Meteor.publish('booksWithPoems', function (bookId) {
  const book = Books.findOne({ _id: bookId });
  return [
    Books.find({ _id: bookId }),
    Poems.find({
      _id: {
        $in: book.poems.map( p => p.id ), // ["12fjenofnoi23","563jdfb34jfrr","78a1ewqeqweq"]
      }
    })
  ];
});

现在,应该选择分配给$in的所有诗歌,其ID与数组中提供的ID之一匹配.

Now it should select all poems, whose id fits one of the ids provided in the array, assigned to $in.

我尚未检查客户端代码和连接数据的方式,但我认为此mongo查询选择器是此处的主要问题.

I have not checked the client code and the way you join the data, but I think this mongo query selector is the main issue here.

有关$ in的更多信息: https://docs.mongodb .com/manual/reference/operator/query/in/

More to read on $in: https://docs.mongodb.com/manual/reference/operator/query/in/

这篇关于使用Meteor JS在MongoDB中创建两个集合的联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆