Mongo / Mongoose查询在Mongo shell中工作但在Node中不起作用(在EC2 Ubuntu映像上)

时间:2018-02-07 03:56:18

标签: node.js mongodb amazon-ec2 mongoose

我很难确定此查询在Mongo shell中的工作原理,但在我尝试从浏览器访问服务器端点时却没有。

Shell命令:

db.playlists.find({keywords: {"$in" : ["featured"]}}).limit(5)

服务器代码:

app.get('/getFeaturedPlaylists', (req, res) => { 
  let query = {
    keywords: { "$in" : ["featured"]}
  }
  database.Playlist.find({query}).limit(5).exec(function(err, data) {                 
    err ? console.log(err) : res.send({body: JSON.stringify(data)});
  })
})

预期结果是一系列播放列表对象:

{"body": "[{\"songList\":[{\"songId\":522,\"bpm\":45},{\"songId\":310,\"bpm\":45},{\"songId\":199,\"bpm\":90},{\"songId\":170,\"bpm\":150},{\"songId\":17,\"bpm\":150},{\"songId\":722,\"bpm\":45},{\"songId\":767,\"bpm\":90},{\"songId\":754,\"bpm\":120},{\"songId\":844,\"bpm\":150},{\"songId\":422,\"bpm\":120},{\"songId\":638,\"bpm\":90},{\"songId\":243,\"bpm\":150},{\"songId\":524,\"bpm\":90},{\"songId\":575,\"bpm\":120},{\"songId\":143,\"bpm\":120}],\"shuffledQueue\":[],\"keywords\":[\"bluegrass\",\"featured\"],\"_id\":\"5a76188fde76d97bda5e342e\",\"playlistId\":2,\"playlistName\":\"Playlist2\"},{\"songList\":[{\"songId\":401,\"bpm\":45},{\"songId\":536,\"bpm\":150},{\"songId\":982,\"bpm\":60},{\"songId\":812,\"bpm\":60},{\"songId\":466,\"bpm\":150}],\"shuffledQueue\":[],\"keywords\":[\"featured\",\"bluegrass\"],\"_id\":\"5a76188fde76d97bda5e3432\",\"playlistId\":6,\"playlistName\":\"Playlist6\"},{\"songList\":[{\"songId\":248,\"bpm\":120},{\"songId\":84,\"bpm\":45},{\"songId\":53,\"bpm\":60},{\"songId\":749,\"bpm\":45},{\"songId\":811,\"bpm\":120}],\"shuffledQueue\":[],\"keywords\":[\"featured\",\"hip-hop\"],\"_id\":\"5a76188fde76d97bda5e3433\",\"playlistId\":7,\"playlistName\":\"Playlist7\"},{\"songList\":[{\"songId\":894,\"bpm\":150},{\"songId\":190,\"bpm\":60},{\"songId\":235,\"bpm\":60},{\"songId\":632,\"bpm\":60},{\"songId\":970,\"bpm\":60},{\"songId\":475,\"bpm\":90},{\"songId\":304,\"bpm\":60},{\"songId\":816,\"bpm\":120},{\"songId\":613,\"bpm\":150},{\"songId\":310,\"bpm\":45},{\"songId\":599,\"bpm\":45},{\"songId\":91,\"bpm\":90},{\"songId\":650,\"bpm\":120},{\"songId\":219,\"bpm\":45},{\"songId\":290,\"bpm\":90}],\"shuffledQueue\":[],\"keywords\":[\"hip-hop\",\"featured\"],\"_id\":\"5a76188fde76d97bda5e3436\",\"playlistId\":10,\"playlistName\":\"Playlist10\"},{\"songList\":[{\"songId\":778,\"bpm\":60},{\"songId\":652,\"bpm\":60},{\"songId\":792,\"bpm\":90},{\"songId\":353,\"bpm\":120},{\"songId\":528,\"bpm\":60},{\"songId\":887,\"bpm\":120},{\"songId\":287,\"bpm\":90},{\"songId\":926,\"bpm\":120},{\"songId\":671,\"bpm\":90}],\"shuffledQueue\":[],\"keywords\":[\"featured\",\"reggae\"],\"_id\":\"5a76188fde76d97bda5e3437\",\"playlistId\":11,\"playlistName\":\"Playlist11\"}]"}

实际结果是一个空数组:

{"body":"[]"}

更奇怪的是,当我将服务器和数据库移动到EC2实例时,才出现此问题。在我的本地机器上使用完全相同的代码和数据集,它会按照我预期的方式响应(通过Postman或Chrome - 这就是我获得上述“预期响应”片段的方式)。此外,指向此数据库的所有其他服务器端点都可以工作,但它们只查找单个播放列表。

对此有任何见解将不胜感激,当然我很乐意根据需要提供更多详细信息。

谢谢!

1 个答案:

答案 0 :(得分:1)

这是问题,

 database.Playlist.find({query})

您没有传递查询对象,而是使用{ }传递空对象{query}

改为执行此操作,

 database.Playlist.find(query)

希望这能解决问题。