简体   繁体   中英

How do I query a map of subdocuments by a key in MongoDB?

I've created the following mongoose schema to save some music data that I pull from 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 ]
  }
});
...

search field in SearchResultSchema takes a search query as the key (eg 'The Beatles'), and stores the returned results as the array of subdocuments of MusicAlbumSchema. In order to test it, I tried to write some mocha tests:

...
  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();
      });
    });
  });
...

What am I doing wrong and how do I properly query the saved data (particularly - search field in SearchResultSchema)?

Decided to change my schema to this:

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]
});

– and, consequently, the mocha test changed as well:

  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();
      });
    });
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM