我有两个收藏,比如cars
和owners
。一个owner
有很多cars
。而one car
只属于one owner
。当车主死亡时,我需要从cars
集合中删除所有车辆。为了做到这一点,我在js中创建了脚本文件。 aliveOwners
是undefined
,这样做的最好方法是什么?是否有可能使用Mongoose
?如果在其他收藏中找不到物品,请从收藏中删除物品
MongoClient.connect(url, (err, db) => {
assert.equal(null, err);
console.log('Connected successfully to server');
var aliveOwners = db.collection('owner').find({}); // TODO. it should get only owner ids
console.log(aliveOwners); // undefined, mb cuz of non blocking
db.collection('cars').remove({ ownerId: { $nin: aliveOwners } })); //TODO. delete cars if owner id does not exist in aliveOwners
});
你为什么不简单地把车内的细节嵌入车主的首位呢?这应该是你使用MongoDB的原因之一,如果你只是想把它当作一个RDBMS来处理,那就没什么意义了。你永远不可能违反16MB的自有车。即使你是Jay Leno –