Skip to content

Instantly share code, notes, and snippets.

@lineus lineus/hillct4.js Secret
Created Aug 27, 2018

Embed
What would you like to do?
I would just overwrite the entire array
#!/usr/bin/env node
'use strict';
const assert = require('assert');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const schema = new Schema({
arr: [{ val: Number, str: String }]
});
const Test = mongoose.model('test', schema);
const test = new Test;
for (let i = 0; i < 5; i++) {
test.arr.push({val: i, str: `${i}`});
}
async function run() {
await conn.dropDatabase();
await test.save();
let doc = await Test.findOne();
assert.deepEqual(doc.arr.map(v => v.val), [0,1,2,3,4]);
assert.deepEqual(doc.arr.map(v => v.str), ['0', '1', '2', '3', '4']);
doc.arr = doc.arr.sort((a,b) => { return b.val - a.val;});
await doc.save();
let updated = await Test.findOne();
assert.deepEqual(updated.arr.map(v => v.val), [4,3,2,1,0]);
assert.deepEqual(updated.arr.map(v => v.str), ['4', '3', '2', '1', '0']);
console.log('All assertions passed.');
return conn.close();
}
run();
$: ./hillct4.js
All assertions passed.
$:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.