Last active
November 5, 2018 08:49
-
-
Save bangbang93/083711c487b61237a09d2d789954f42b to your computer and use it in GitHub Desktop.
mongoose-findbyidandupdate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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