Skip to content

Instantly share code, notes, and snippets.

@mariohmol
Last active June 29, 2018 17:45
Show Gist options
  • Save mariohmol/d452a9de59e9da5204f67f7423d22557 to your computer and use it in GitHub Desktop.
Save mariohmol/d452a9de59e9da5204f67f7423d22557 to your computer and use it in GitHub Desktop.
example showing created subdoc ids
#!/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({
arr: [{ type: mongoose.Schema.Types.Mixed }]
});
const Test = mongoose.model('test', schema);
const test = new Test({
arr: [{ x: '1' }]
});
async function run() {
await conn.dropDatabase();
await test.save();
let newVals = [{ x: '2'}, { x: '3'}];
let cond = { _id: test._id };
let upd8 = { $push: { arr: { $each: newVals } } };
let opts = { new: true };
await Test.findOneAndUpdate(cond, upd8, opts);
let doc = await Test.findOne(cond);
console.log(doc);
return conn.close();
}
run();
$: ./mario.js
{ _id: 5b212d614ac1fe315db489ed,
arr:
[ { _id: 5b212d614ac1fe315db489ee, x: '1' },
{ _id: 5b212d624ac1fe315db489f0, x: '2' },
{ _id: 5b212d624ac1fe315db489ef, x: '3' } ],
__v: 0 }
$:
#!/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({
arr: [{ type: mongoose.Schema.Types.Mixed }]
});
const Test = mongoose.model('test', schema);
const test = new Test({
arr: [{ x: '1' }]
});
let newVals = [{ x: '2' }, { x: '3' }];;
let cond = { _id: test._id };
let upd8 = { $push: { arr: { $each: newVals } } };
let opts = { new: true };
conn.dropDatabase()
.then(c => {
return test.save();
})
.then(c => {
return Test.findOneAndUpdate(cond, upd8, opts);
})
.then(c => {
return Test.findOne(cond);
})
.then(c => {
let doc = c;
console.log(doc);
return conn.close();
})
.then(c => { });
#!/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({
name: String,
arr: [{ type: mongoose.Schema.Types.Mixed }]
});
// arr
schema.index({ name: 1, arr: "text" }, { name: "TextIndex" });
const Test = mongoose.model('test', schema);
const test1 = new Test({
name: 'tabela mario',
arr: [
{ x: 123, d: new Date(), text: 'olaaaaa' },
{ x: 234, d: new Date(), text: 'eiii' },
{ x: 456, d: new Date(), text: 'mario' }
]
});
const test2 = new Test({
name: 'tabela equipe',
arr: [
{ x: 321, d: new Date(), text: 'equipe' },
{ x: 432, d: new Date(), text: 'condo' },
{ x: 543, d: new Date(), text: 'eiii' }
]
});
const search = {
$text: {
$search: 'eiii',
$caseSensitive: false,
$diacriticSensitive: false
}
}
const searchAgreggate = {
$text: {
$search: 'eiii',
$caseSensitive: false,
$diacriticSensitive: false
},
name: 'tabela mario'
}
conn.dropDatabase()
.then(c => {
return Promise.all([test1.save(), test2.save()]);
})
.then(c => {
return Test.find(searchAgreggate);
})
.then(c => {
console.log(c);
return Test.aggregate(
[
{ $match: searchAgreggate },
{ $sort: { score: { $meta: "textScore" } } },
{ $project: { name: 1, _id: 0 } }
]
)
})
.then(e=>{
console.log(e)
return conn.close();
})
.catch(e=>{
console.log(e);
conn.close();
})
#!/usr/bin/env node
'use strict';
const util = require('util')
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const schema = new Schema({
name: String,
arr: [{ type: mongoose.Schema.Types.Mixed }]
});
// arr
schema.index({ name: 1, '$**': "text" }, { name: "TextIndex" });
const Test = mongoose.model('test', schema);
const test1 = new Test({
name: 'tabela mario',
arr: [
{ x: 123, d: new Date(), text: 'olaaaaa' },
{ x: 234, d: new Date(), text: 'eiii' },
{ x: 456, d: new Date(), text: 'mario' }
]
});
const test2 = new Test({
name: 'tabela equipe',
arr: [
{ x: 321, d: new Date(), text: 'equipe' },
{ x: 432, d: new Date(), text: 'condo' },
{ x: 543, d: new Date(), text: 'eiii' }
]
});
const search = {
$text: {
$search: 'eiii',
$caseSensitive: false,
$diacriticSensitive: false
}
}
const searchAgreggate = {
$text: {
$search: 'eiii',
$caseSensitive: false,
$diacriticSensitive: false
},
name: 'tabela mario'
}
conn.dropDatabase()
.then(c => {
return Promise.all([test1.save(), test2.save()]);
})
.then(c => {
return Test.find(searchAgreggate);
})
.then(c => {
return Test.aggregate(
{ $match: { name: 'tabela mario' } },
{ $unwind: '$arr' },
{
$match: {
$or: [
{ 'arr.text': 'eiii' },
{ 'arr.text': 'mario' }
]
}
},
{ $sort: { 'arr.x': -1 } },
{ $group: { _id: '$_id', arr: { $push: '$arr' } } })
})
// .then(c => {
// console.log(c[0])
// util.inspect(c, false, null);
// return Test.aggregate(
// [
// { $match: searchAgreggate },
// // { $unwind: '$arr'},
// { $sort: { score: { $meta: "textScore" } } }
// ]
// )
// })
.then(e => {
console.log(e[0])
util.inspect(e, false, null)
return conn.close();
})
.catch(e => {
console.log(e);
conn.close();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment