【发布时间】:2020-08-20 00:06:35
【问题描述】:
async deleteRecipeById(recipeId){
try{
const client = new MongoClient(this.uriString);
await client.connect();
const db = client.db(this.databaseString);
const recipeCollection = db.collection(this.collectionString);
//console.log(await recipeCollection.find({recipeName:recipeName}).toArray());
var recipeIdAsObject = new ObjectId(recipeId);
var result = recipeCollection.deleteMany({"_id":recipeIdAsObject});
client.close();
return result;
}
catch(e){
console.error(e);
}
}
我正在测试我的 mongo 驱动程序,但我终其一生都无法弄清楚为什么这不起作用。我用我的 deleteRecipeByName() 运行相同的测试并且测试通过了。所以这对我来说表明我的测试夹具很好。这意味着我知道该配方存在于具有正确 ID 的数据库中。
在我运行测试之前,我什至调用了一个我已经测试过的 getRecipeByName 函数,以确保配方与我正在寻找的 ID 存在并且这就是结果。
[
{
_id: 0,
recipeName: 'mock0',
recipeIngredients: [ 'i0', 'i1', 'i2' ]
}
]
还有我的测试功能
describe('Testing delete Recipe',()=>{
it('1. Delete known recipe',(done)=>{
var uri = "mongodb://localhost:27017";
var dbname = "testRecipes";
var collectionName = "testCollectionMessy";
let driver = new MongoDriver(uri,dbname,collectionName);
driver.dropCollection().then(()=>{//Clean collection for testing... NEVER CALL ON PRODUCTION COLLECTION
driver.addMockData().then((p)=>{
driver.getRecipeById(mockData[0]._id).then((p)=>{
console.log(p);
})
driver.deleteRecipeById(0).then((p)=>{
console.log(p);
console.log(mockData[0]._id);
assert.deepEqual(p.deletedCount,1);
done();
}).catch((e)=>{
console.log(e);
done(e);
})
});
});
})
})
这是我从 JSON 导入的 mockData
[
{"_id":0,"recipeName":"mock0","recipeIngredients":["i0","i1","i2"]},
{"_id":1,"recipeName":"mock1","recipeIngredients":["i0","i1","i2"]},
{"_id":2,"recipeName":"mock2","recipeIngredients":["ingredient"]}
]
【问题讨论】:
-
如果你运行
new ObjectId(recipeId),其中recipeId是0, 1 or 2,它会抛出一个错误 -
所以我不能拥有 0-2 值的 ID?
-
你需要避免创建
recipeIdAsObject而直接使用receipeId -
非常感谢您的成功。我需要更改我的测试夹具,所以我使用更类似于我的生产环境的 id。
-
添加了支持两种格式的答案
标签: javascript node.js mongodb mongojs