These are chat archives for Automattic/mongoose

1st
Jul 2018
Jake Lees
@brocococonut
Jul 01 2018 08:18
Hey :) I was just wondering if I could grab some help with something.
I wanted to do something in one query instead of splitting it into two.
I wanted to run $addToSet if the item doesn't exist in an array of an object and $pull if it does already exist.
The only way I can think of doing this is possibly by running some an aggregate query, or by doing two seperate queries
Jake Lees
@brocococonut
Jul 01 2018 08:25
Or I guess through the query builder… Hmmm
SZun
@SZun
Jul 01 2018 12:58
Hello, I'm having an issue with mongoose creating a database. I've used mongoose before and never ran into any issues. But for some reason it is not working at the moment. I can create databases using my command line as well as MongoDB Compass. I also added some basic promise-based error handling which has not returned any errors. Can someone help me figure out what the issue is?
Kev
@lineus
Jul 01 2018 12:59
@SZun what version of mongoose are you using?
SZun
@SZun
Jul 01 2018 13:01
@lineus 5.1.7 and I've also tried 5.0.1 for a Udemy course as well
Kev
@lineus
Jul 01 2018 13:02
how are you creating the database in mongoose? can you share a code sample?
SZun
@SZun
Jul 01 2018 13:03
Sure, can I send code snippets in here or just raw text?
Kev
@lineus
Jul 01 2018 13:03
if it's more than 30 lines you can create a gist at http://gist.github.com
if it's less, you can wrap your code here in three backticks
```
code
```
SZun
@SZun
Jul 01 2018 13:06

```const mongoose = require("mongoose");

mongoose.connect("mongodb://localhost/playground")
.then(() => console.log(Connected to MongoDB...))
.catch(err => console.log(Could not connect to MongoDB,err));```

Kev
@lineus
Jul 01 2018 13:06
to get formatting the backticks need to be on their own lines
just fyi
and you never see either message?
SZun
@SZun
Jul 01 2018 13:08
const mongoose  =  require("mongoose");
mongoose.connect("mongodb://localhost/playground")
    .then(() => console.log(`Connected to MongoDB...`))
    .catch(err => console.log(`Could not connect to MongoDB`,err));
I get the Connected to MongoDB... back
Kev
@lineus
Jul 01 2018 13:09
ahh
the database won't be created until you create a collection
the easiest way to do this is to save a document
SZun
@SZun
Jul 01 2018 13:11
Ohhh okay thank you!! I'm doing a Udemy course on Node-Express-MongoDB REST API's and the guy had the database show up right after he ran almost the exact same code so I was worried. I'm gonna create a collection and see if it works. Thanks!!
SZun
@SZun
Jul 01 2018 13:14
Just ran it and it worked perfectly, thank you so much!!
Kev
@lineus
Jul 01 2018 13:15
happy to help :smile:
Lamp
@ledlamp
Jul 01 2018 21:16
I need to make a virtual getter that queries another collection; does it support async functions?
Lamp
@ledlamp
Jul 01 2018 21:36
it doesn't seem to be working, i'm just getting an empty object in json string
worldSchema.virtual('creatorName').get(async function(){
    var acc = await Account.findOne({id: this.creatorID});
    return acc.username;
});
image.png
hold up i havent required Account
still same thing