your syntax looks good to me
#!/usr/bin/env node | |
'use strict'; | |
const mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/test'); | |
const conn = mongoose.connection; | |
const Schema = mongoose.Schema; | |
const schema = new Schema({ | |
events: [Schema.Types.ObjectId] | |
}); | |
const Test = mongoose.model('test', schema); | |
const test = new Test({ | |
events: [ | |
mongoose.Types.ObjectId(), | |
mongoose.Types.ObjectId(), | |
mongoose.Types.ObjectId() | |
] | |
}); | |
const toRemove = test.events[2]; | |
console.log(`removing: ${toRemove}`); | |
async function run() { | |
await conn.dropDatabase(); | |
await test.save(); | |
await Test.findByIdAndUpdate(test._id, { $pull: { events: toRemove } }); | |
let found = await Test.findOne(); | |
console.log(found); | |
return conn.close(); | |
} | |
run(); |
$: ./benydc.js | |
removing: 5b195ff3cbf271283f9cbcbf | |
{ events: [ 5b195ff3cbf271283f9cbcbd, 5b195ff3cbf271283f9cbcbe ], | |
_id: 5b195ff3cbf271283f9cbcc0, | |
__v: 0 } | |
$: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment