文章目录

  • ​​连接​​
  • ​​聚合查询 (和node-mongodb一样)​​

连接

const mongoose = require('mongoose');

const options = {
host: "159.75.22.82",
port: "27017",
db_name: "xxx",
user: "xxx",
pass: "xxx",
};

const url = `mongodb://${options.user}:${options.pass}@${options.host}:${options.port}/${options.db_name}`;

// useUnifiedTopology: true,
const MongoClientConfig = {
dbName: 'uglyTuan'
};

mongoose.connect(url, MongoClientConfig).then(err => {
// console.log('err', err);
})

const Com = mongoose.model('comment', new mongoose.Schema(), "comment");

console.log("COM:", Com.find((err, res) => {
console.log(err, res)
}))

mongoose_node.js

聚合查询 (和node-mongodb一样)

const comments = await ctx.model.Comment.aggregate([
{
$lookup: {
from: 'user',
localField: 'sender',
foreignField: '_id',
as: 'sender'
}
},
{ $limit: 20 }
]);