Skip to content

Instantly share code, notes, and snippets.

@bangbang93
Last active November 5, 2018 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bangbang93/083711c487b61237a09d2d789954f42b to your computer and use it in GitHub Desktop.
Save bangbang93/083711c487b61237a09d2d789954f42b to your computer and use it in GitHub Desktop.
mongoose-findbyidandupdate
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Mongoose = require("mongoose");
Mongoose.connect('mongodb://lo-srv-0.s.chipwing.com/test');
const schema = new Mongoose.Schema({
_id: {
type: Number,
},
a: {
unique: true,
type: String,
}
});
const model = Mongoose.model('t', schema);
async function mongoose() {
function fn(a) {
return model.findByIdAndUpdate(1, { $set: { a } }, { upsert: true })
.exec();
}
fn('a');
fn('b');
}
async function mongodb() {
const collections = Mongoose.connection.collection('ts');
function fn(a) {
return collections.findOneAndUpdate({ _id: 2 }, { $set: { a } }, { upsert: true });
}
fn('c');
fn('d');
}
mongoose();
mongodb();
import * as Mongoose from 'mongoose'
Mongoose.connect('mongodb://lo-srv-0.s.chipwing.com/test')
const schema = new Mongoose.Schema({
_id: {
type: Number,
},
a: {
unique: true,
type: String,
}
})
const model = Mongoose.model('t', schema)
async function mongoose() {
function fn(a) {
return model.findByIdAndUpdate(1, {$set: {a}}, {upsert: true})
.exec()
}
fn('a')
fn('b')
}
async function mongodb() {
const collections = Mongoose.connection.collection('ts')
function fn(a) {
return collections.findOneAndUpdate({_id: 2}, {$set: {a}}, {upsert: true})
}
fn('c')
fn('d')
}
mongoose()
mongodb()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment