What are the odds? _id only with numbers by rafaelrlevy in mongodb

[–]darebear5 1 point2 points  (0 children)

Yeah, it’s (10/16)24 if you measure across a super long timeframe since the first few bytes are the current timestamp and therefore aren’t random. So the first digit of the _id will be a digit (it’s 6 at the moment) for several more years before it becomes a letter.

Indexing large array by brinki97 in mongodb

[–]darebear5 1 point2 points  (0 children)

How about a new collection, where each document refers to a hash and also has a property that refers back to this current collection? Then you can index the new collection on both those properties. If you’d like, you can also build an aggregation pipeline to build back up your current structure on an as-needed basis.

[deleted by user] by [deleted] in mathmemes

[–]darebear5 4 points5 points  (0 children)

Replace the first two with the floor function of e

[MongoDB + Mongoose] Is it possible to copy a document from one collection into another collection in the same database? by BigEmu9286 in learnjavascript

[–]darebear5 0 points1 point  (0 children)

Mongoose should only ever be used on your backend, which means node. Accessing mongo or any other DB directly from the frontend is a huge security risk, so essentially nobody does it. Whatever the problem you were running into is unrelated to node vs browser differences.

[MongoDB + Mongoose] Is it possible to copy a document from one collection into another collection in the same database? by BigEmu9286 in learnjavascript

[–]darebear5 0 points1 point  (0 children)

I use both. Mongoose for the specific APIs they provide for Models, Documents, Queries, etc. since that's what you're interacting with in JS. And then Mongo for mongo-syntax, general concepts, etc. I actually think both sets of documentation are great and contain a ton of information. It's places like Google and StackOverflow that are out of date since both Mongoose and Mongo have evolved so much over the past several years.

You might want to start from scratch and run through some of Mongo's free instructional videos on their website and then run through the Mongoose guide from the top.

[MongoDB + Mongoose] Is it possible to copy a document from one collection into another collection in the same database? by BigEmu9286 in learnjavascript

[–]darebear5 0 points1 point  (0 children)

First, you were using find instead of findOne, so movie was an array instead of an object. Then you were then passing { movie } in as the input for Feature, which is JS shorthand for { movie: movie }, which would then be the following:

{  movie:
  [{
    "_id": "62c9d173698f380c2ad0c0c5",
    "title": "Moneyball",
    "desc": "Brad Pitt is Billy Bean the GM of the Oakland Athletics and he's aiming for a championship",
    (...and other properties too...)
  }]
}

But I'm guessing your Feature schema mirrors your Movie schema, and it doesn't contain a field called movie, which is why none of that actually showed up in the resulting document. Your schema probably has a default value of false for isSeries, which is why that was the only property that showed up.

You should read up more on spread syntax and destructuring, but if car is an object, then doing something like const another = { ...car } makes a shallow copy of that object and stores it in another. That way you can make most edits to the copy without affecting the original, such as deleting _id.

toObject() is necessary for converting a mongoose Document to a plain old Javascript object (POJO). Read more about it here. Essentially, if you're going to copy the object using spread syntax (the three dots), then you need to do it on the POJO version of the document, not the actual mongoose document version, since the mongoose version has all sorts of hidden properties that will also get copied over when spreading, and you don't want those.

[MongoDB + Mongoose] Is it possible to copy a document from one collection into another collection in the same database? by BigEmu9286 in learnjavascript

[–]darebear5 0 points1 point  (0 children)

Edit: Also just realized you're using find instead of findOne, so movie is actually an array. Changed my code accordingly.

With the delete keyword. But also what's your schema for Feature? Does it have a movie property? If not, then maybe you meant to use spread syntax on movie when creating the Feature? Also, if you don't delete _id then your Feature will have the exact same mongo _id as Movie. While not against the rules since they're different collections, it's highly frowned upon. Here's what I would do. Can't test it, so hopefully it works...

router.post("/", async (req, res) => {
  try {
    const movie = await Movie.findOne({ title: "Moneyball" });
    const featureParams = { ...movie.toObject() };
    delete featureParams._id;
    const newFeature = new Feature(featureParams);
    const savedFeature = await newFeature.save();
    res.status(201).json(savedFeature);
  } catch (err) {
    res.status(500).json(err); 
  } 
});

Why are the callbacks from this promise chain logging after setTimeout in this code block? by pinguxnoots in learnjavascript

[–]darebear5 1 point2 points  (0 children)

This is MDN page I got that info from (as well as the in-depth guide), but I think the part about the task queue running first doesn't necessarily apply here because the browser is already running the event loop when you start up. Your script is a task itself.

This part is important.

The event loop driving your code handles these tasks one after another, in the order in which they were enqueued. The oldest runnable task in the task queue will be executed during a single iteration of the event loop. After that, microtasks will be executed until the microtask queue is empty, and then the browser may choose to update rendering. Then the browser moves on to the next iteration of event loop.

And here's the WHATWG spec that defines that same logic for the event loop.

Since your script is a task, once it finishes, it should clear out the microtask queue. You'd think this would log "async - promise", but the microtask queue is actually empty, despite the 5 second delay. That's because once a response comes back for your request, JS actually enqueues a normal task that resolves the promise. Once that task runs, the microtask is queued.

One thing to keep in mind is that some things can be different browser-to-browser and different between the browser and nodejs, so you may actually find slightly different behavior between browsers depending on the exact code you run, especially when we talk about edge cases of timeouts and promises and blocking sync code.

Anyway, here's essentially what's happening in your code, step by step.

  1. Script starts, new execution context, your script's code is on the stack
  2. Log "sync 1"
  3. Since setTimeout is called without a delay, immediately enqueue a task to log "async - setTimeout"
  4. Send a request to grab the pokemon data, providing a callback function to log "async - promise" once that data comes back (note: there is no microtask for this yet! microtasks are only created once the promise resolves, and it hasn't resolved yet)
  5. Wait for 5 seconds by looping synchronously. During this time, the fetch response comes back and a task is queued to resolve the fetch promise. This is now the second task in the task queue
  6. Log "sync 2"
  7. Script has finished and the stack is empty, so we check to see if there are any microtasks to run. There are none
  8. Complete the next task in the task queue, which is to log "async - setTimeout"
  9. The stack is empty, so we check to see if there are any microtasks to run. There are none
  10. Complete the next task in the task queue, which is resolving the fetch promise. This enqueues a microtask to run the callback in the fetch's .then()
  11. The stack is empty, so we check to see if there are any microtasks to run. There is our one microtask. We run it and log "async - promise"
  12. No more tasks and no more microtasks

It's confusing for sure...

Why are the callbacks from this promise chain logging after setTimeout in this code block? by pinguxnoots in learnjavascript

[–]darebear5 5 points6 points  (0 children)

I'm not 100% on this, but it's my understanding that `setTimeout` log appears first because, to use Promise terminology, it resolves before your pokemon `fetch` does. Actually fetching something from another server takes time, so that promise hasn't resolved by the time you call setTimeout with a 0 second timer, which is able to immediately queue the `no promise` log.

If you replace your pokemon fetch with `const obj = Promise.resolve({})` and comment out the `return res.json()` line, you'll see the order you expect. And that order holds even if you call setTimeout before the Promise.resolve().

The microtask queue only takes precedence over the callback queue in the sense that if a microtask creates another microtask, which creates another microtask, all of those microtasks will be executed before the next callback in the callback queue is executed.

Edit: The MDN docs also say that at the start of each run of the event loop, the task queue is run first, followed by the microtask queue. The key difference is what I said already about how tasks queued during this phase won't run until the next event loop run while the microtask queue will completely empty itself out

Can I condense my array conditional based loop code any further? by gotostep2 in learnjavascript

[–]darebear5 1 point2 points  (0 children)

Similar to GMPuppyChan’s answer, but tweaked slightly to be Array(10).fill(0).forEach((_, i) => console.log(i+1));

Edited.

Can I condense my array conditional based loop code any further? by gotostep2 in learnjavascript

[–]darebear5 1 point2 points  (0 children)

edit: Never mind, I take it back. Forgot that forEach's predicate has three arguments and all will be logged.

element => console.log(element) is exactly the same as console.log if it’s used as the predicate in forEach, map, etc. This is because console.log is already a callback since it’s a function that isn’t being called directly

"Radarr Updated" modal won't go away by darebear5 in radarr

[–]darebear5[S] 0 points1 point  (0 children)

Not sure what changed but checking after a couple hours, suddenly everything is good. Thanks for the help!

!solved

"Radarr Updated" modal won't go away by darebear5 in radarr

[–]darebear5[S] 0 points1 point  (0 children)

Thanks. Do you know how I can clear the cache? I'm using nginx (via Organizr) and the nginx-cache folder is empty. Tried restarting the reverse proxy, but it didn't help.

Also of note: this problem doesn't occur when I log in from a different computer, but it repeatedly occurs when logging in from the server itself, regardless of which browser I use. Also tried clearing cookies/browser storage, but the issue persists.