node.js - 如何通过 MongoDB 中的键查询子文档的映射?

标签 node.js mongodb testing mongoose mocha.js

我创建了以下 Mongoose 模式来保存我从 iTunes API 中提取的一些音乐数据:

...
const MusicAlbumSchema = new Schema({
  artist_name: {
    type: String,
    required: 'enter an artist name'
  },
  album_name: {
    type: String,
    required: 'enter an album name'
  },
  artwork_url: {
    type: String,
    required: 'enter an artwork url'
  }
});
const SearchResultSchema = new Schema({
  created_date: {
    type: Date,
    default: Date.now
  },
  search: {
    type: Map,
    of: [ MusicAlbumSchema ]
  }
});
...
SearchResultSchema 中的

搜索字段将搜索查询作为键(例如“The Beatles”),并将返回的结果存储为 MusicAlbumSchema 的子文档数组。为了测试它,我试着写了一些 mocha 测试:

...
  it('Creates a search result with sub-documents', done => {
    const musicAlbum1 = new MusicAlbum({
      artist_name: 'The Beatles',
      album_name: 'Abbey Road',
      artwork_url: 'www.google.com'
    });
    const musicAlbum2 = new MusicAlbum({
      artist_name: 'The Beatles',
      album_name: 'Let It Be',
      artwork_url: 'www.google.com'
    });

    const result = new SearchResult({
      search: {}
    });
    result.search.set('The Beatles', [musicAlbum1, musicAlbum2]);


    result.save().then(() => {
      SearchResult.findOne({
        'The Beatles' : { $exists : true }
      }).then(records => {
        console.log(records);
        assert(records.length === 2);
        done();
      });
    });
  });
...

我做错了什么以及如何正确查询保存的数据(特别是 SearchResultSchema 中的搜索字段)?

最佳答案

决定将我的架构更改为:

const MusicAlbumSchema = new Schema({
  artist_name: {
    type: String,
    required: true
  },
  album_name: {
    type: String,
    required: true
  },
  artwork_url: {
    type: String,
    required: true
  }
});

const SearchResultSchema = new Schema({
  created_date: {
    type: Date,
    default: Date.now
  },
  search_query: {
    type: String,
    required: true,
    unique: true,
    dropDups: true
  },
  search_results: [MusicAlbumSchema]
});

——因此,mocha 测试也发生了变化:

  it('Creates a search result with sub-documents', done => {
    const musicAlbum1 = new MusicAlbum({
      artist_name: 'The Beatles',
      album_name: 'Abbey Road',
      artwork_url: 'www.google.com'
    });
    const musicAlbum2 = new MusicAlbum({
      artist_name: 'The Beatles',
      album_name: 'Let It Be',
      artwork_url: 'www.google.com'
    });

    const result = new SearchResult({
      search_query: 'The Beatles',
      search_results: [musicAlbum1, musicAlbum2]
    });

    result.save().then(() => {
      SearchResult.findOne({
        search_query: 'The Beatles'
      }).then(record => {
        assert(record.search_results.length === 2);
        done();
      });
    });
  });

关于node.js - 如何通过 MongoDB 中的键查询子文档的映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56031240/

相关文章:

javascript - 在 Electron 中加载本地html文件的简单方法

node.js - 如何从 Node 正确调用AWS Api Gateway?

node.js - 如何使用 Spring Cloud 和 Netflix 的 Eureka 注册 Node 应用程序

javascript - 如何在没有 MongoLab 的情况下将本地 MongoDB 数据库连接到我的 Android Studio 应用程序?

mongodb - Golang MongoDB 不返回条件查询

node.js - 如何在 NodeJS + Express 4 中获取 req.user.email

node.js - 如何在 MongoDB 和 Node 的另一个集合中引用 ObjectID?

python - 当我的项目(Django)发生任何变化时如何自动运行测试?

在 pre-IOS4 上测试

android - Android Studio 的 "No tests were found"