[英]how add elements in collection with Mongoose
I try to learn databases, specially mongoDb.我尝试学习数据库,特别是 mongoDb。
I use Mongoose and i try to add elements in my collection.我使用 Mongoose 并尝试在我的集合中添加元素。 I know that's a basic question but i don't understand where can be my problem
我知道这是一个基本问题,但我不明白我的问题在哪里
there is my code:有我的代码:
mongoose.connect("mongodb://localhost:27017/hackathon", (error)=>{
if (!error){
console.log("YESSS")
}
else {
console.log("rip")
}
var friend = { firstName: 'Harry', lastName: 'Potter' };
mongoose.connection.db.listCollections().toArray(function (err, names) {
console.log(names); // [{ name: 'dbname.myCollection' }]
module.exports.Collection = names;
});
});
the mongoose.connect function correctly, and the console.log gives me mongoose.connect function 正确,console.log 给了我
{ name: 'mycol',
type: 'collection',
I tried to do:我试着做:
collection.push but he doesn't know it. collection.push 但他不知道。 i tried something like that
我试过类似的东西
var dbo = db.db("mycol");
dbo.collection("mycol").insertOne(friend, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
(i found it on stack overflow but that do nothing) Is there something i miss? (我在堆栈溢出时发现它,但什么也没做)有什么我想念的吗?
I think it's an easy thing and i just miss something in my comprehension.我认为这是一件容易的事情,我只是错过了一些我的理解。
Sorry for my english and thanks !对不起我的英语,谢谢!
I used an other technique, i don't know if its the best solution so i created a schema (with differents elements, name, number-phone etc), and with a new and save, i found my solution.我使用了另一种技术,我不知道它是否是最好的解决方案,所以我创建了一个模式(具有不同的元素、名称、电话号码等),并通过新的保存,我找到了我的解决方案。
Thanks for your answers感谢您的回答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.