我有一個具有以下結構的 MongoDB 集合:/* 1 */{ "_id" : ObjectId("5cdb24b41a40ae58e6d690fd"), "versions" : [ ObjectId("5cdb24b41a40ae58e6d690fe") ], "releases" : [], "monetization" : [], "owner" : "testuser", "name" : "test-repo-2", "repoAddress" : "/testuser/test-repo-2", "repoKey" : null, "__v" : 0}/* 2 */{ "_id" : ObjectId("5cdb23cb1a40ae58e6d690fa"), "versions" : [ ObjectId("5cdb23cb1a40ae58e6d690fb"), ObjectId("5cdda9c54e6d0b795a007960") ], "releases" : [ ObjectId("5cdda9c54e6d0b795a00795c") ], "monetization" : [], "owner" : "testuser", "name" : "test-repo-1", "repoAddress" : "/testuser/test-repo-1", "repoKey" : null, "__v" : 2, "createdAt" : ISODate("2019-05-16T18:19:49.159Z"), "updatedAt" : ISODate("2019-05-16T18:19:49.252Z")}我需要遍歷集合中的所有文檔以及它們的版本數組,以查找特定的以將其與項目匹配。我需要用 NodeJS 來做這件事,但現在我正在從 mongoshell 嘗試它。我正在嘗試使用forEach()和$in操作符來做到這一點。db.projects.find().forEach( function () { { versions: { $in: ['5cdb24b41a40ae58e6d690fe'] } } });但是每次我收到以下回復時:Script executed successfully, but there are no results to show.我這樣做正確嗎?
循環遍歷集合中的所有文檔和每個文檔中的數組,以將數組值與項目匹配
慕絲7291255
2021-06-01 13:02:46