Skip to content

Instantly share code, notes, and snippets.

@lineus lineus/beardedgardener.js Secret
Created Jul 11, 2018

Embed
What would you like to do?
overwrite: true lets you blow away a createdAt timestamp
#!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
// mongoose.set('debug', true);
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/test', { useMongoClient: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const schema = new Schema({
name: String
}, {timestamps: true});
const Test = mongoose.model('test', schema);
const test = new Test({ name: 'one'});
async function run() {
await conn.dropDatabase();
await test.save();
let date = Date.now() - (3600 * 24 * 30000);
let cond = { _id: test._id };
let update = { $set: { createdAt: new Date(date) } };
let opts = { overwrite: true };
await Test.findOneAndUpdate(cond, update, opts);
let found = await Test.findOne({ _id: test._id });
console.log(`createdAt: ${found.createdAt}`);
return conn.close();
}
run();
$: ./beardedgardener.js
createdAt: Mon Jun 11 2018 18:50:40 GMT-0400 (EDT)
$:
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.