mongoDB does not add new data directly into the root of the document

Here is the scenario I am dealing with:

var temp = Collection.find({nom: "lol"}).fetch();

I made changes to the _id in this instance

Collection2.insert(temp);

After these operations, my MongoDB data looks like this:

{
    "0" : {
       _id: "65462984521651" //<- The updated id
       //Additional information
       //....
      },
    _id : "dvhssdhvflidsfjhv"
}

How can I insert the document into the root level instead of under "0"? Thank you for your help :)

Answer №1

The cursor.fetch method always results in an array being returned. The collection.insert method requires a single document, unlike the native MongoDB JS driver which allows bulk-inserts.

If you are looking to insert multiple documents, there is a discussion on this topic at Does inserting multiple documents in a Meteor Collection work the same as pure mongodb?.

You may want to try the following approach (instead of Collection2.insert(temp)):

temp.forEach(function(doc) {
    Collection2.insert(doc);
});

It is also recommended to use more meaningful names for your variables than just temp. Consider providing context such as temp posts, temp messages, or temp usernames. Similarly, consider giving descriptive names to Collection and Collection2. However, these variable names could be specific to this Stack Overflow question, so adjust accordingly!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Add content to the beginning and end of the page depending on the user's browser

Looking for a way to optimize a function that moves an HTML element within the DOM based on window size. The current implementation uses prepend and append to adjust the position of the image, but since the function is triggered every time the window is re ...

Python Selenium: Cannot Click on Element - Button Tag Not Located

TL,DR: My Selenium Python script seems to be having trouble "clicking" on the necessary buttons. Context: Hello. I am working on automating the process of logging into a website, navigating through dropdown menus, and downloading a spreadsheet. Despite ...

search for multiple IDs in MongoDB within an array of records

I am looking to retrieve objects from the database that correspond to each element in an array of IDs: ["c5f2d584-60ab-4068-b567-9b422f6c4e24", "09c0ef39-55ea-45b4-88d3-a8f97730d6d3"]. Can someone guide me on how to construct this query? https://i.sstati ...

What is the best approach for handling @RequestParam in a JSP file?

Hello there! I have a query regarding the usage of @RequestParam in @RestController. My question is about extracting @RequestParam from the client side. Below is an example of server code using @RestController: @ResponseBody @RequestMapping(method = Reque ...

augmentable form that can be expanded flexibly

Perhaps I'm missing something really simple here, but I just can't seem to figure this out. I'm attempting to create a form that can dynamically extend. The issue I'm facing is that I can't get this particular code to function: & ...

The output from the lodash sortBy function is not aligning with the anticipated result

Sorting this array based on the attributes age and user has become my current challenge. The priority is to first sort by age, followed by sorting by user. In cases where the ages are the same, the sorting should be done based on the user attribute. var us ...

What is the best way to enable users to edit the placeholder text in an HTML input field?

I'm dealing with a situation where I have the following code snippet: <input placeholder="something"> Typically, the placeholder text disappears as soon as the user starts typing. Is there a way to make it so that the placeholder text becomes ...

What could be causing the failure to retrieve the salt and hash values from the database in NodeJS?

My current issue involves the retrieval of hash and salt values from the database. Although these values are being stored during sign up, they are not being retrieved when needed by the application. Below, you will find snapshots of the database, console s ...

Tips for triggering a click event on a hyperlink using a JavaScript function within the Nightmare library

I'm trying to figure out how to click a button using Nightmare with specific attributes. <a class="auth_button leftbtn" href="javascript:SubmitAuthCode( 'enter a friendly name here' );"> <h3>Submit</h3> < ...

Query parameter for spring-data-mongodb that is not required

Currently, I am utilizing spring-data-mongodb to interact with the database. My goal is to be able to query the database by including some optional parameters in my queries. Within my domain class: public class Doc { @Id private String id; ...

Three.js: It seems that THREE.WebGLRenderer detected the image is not a power of two, originally sized at 1600x900. It was resized to 102

As I dive into learning three.js, one of my goals is to incorporate a 16x9 photo into my scene. Below is the snippet of code where I add an Array of images to my scene: const material = new MeshBasicMaterial({ map: loader.load(images[i]), trans ...

Distinguishing between findOneAndDelete() and findOneAndRemove()

I am struggling to understand the difference between findOneAndDelete() and findOneAndRemove() functions in the mongoose documentation. Query.prototype.findOneAndDelete() There is a slight difference between this function and Model.findOneAndRemove(). ...

Can a directive be designed to function as a singleton?

Our large single page app includes a directive that is utilized in various locations across the page, maintaining its consistent behavior and appearance each time. However, we are experiencing an issue with this directive due to the ng-repeat it contains, ...

Tips for accessing the 'Show More' feature on a webpage with lazy loading functionality

My aim is to click the 'Show More' button on a website. I have written this code, but an error occurs below it. from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait #Launch Chrome driver=webdriver.Chrome(execut ...

Using React Native to dynamically change color based on API response

I'm currently working on a React Native project and I have a requirement to dynamically change the background color of a styled component based on the value retrieved from an API. However, I'm facing some challenges in implementing this feature. ...

Integrating webpack with kafka-node for seamless communication between front

I am in the process of embedding a JavaScript code that I wrote into an HTML file. The script requires kafka-node to function properly, similar to the example provided on this link. To achieve this, I am using webpack to bundle everything together. I am fo ...

Invoking an asynchronous method of the superclass from within an asynchronous method in the subclass

I'm currently developing JavaScript code using ECMAScript 6 and I'm facing an issue with calling an asynchronous method from a superclass within a method of an extending class. Here is the scenario I'm dealing with: class SuperClass { c ...

The error message states: `discord.js TypeError: Unable to access the property 'resolve' as it is undefined`

Encountering an issue with the following code snippet const Discord = require('discord.js'); module.exports = { name: 'info', description: "Shows BOT's Informations", execute(message, client, args) { c ...

Getting started with Next.js, only to hit a dead end with a

After spending a week working on Next.js, I decided to test it outside of the development setup. Despite encountering some bugs, I was able to build and start it without any errors. However, when I tried to access http://localhost:3000, I received the foll ...

Is it possible to create a subclass component to customize the event handler?

Looking to modify the behavior of a complex React component (specifically, Combobox from react-widgets)? I want to customize the onKeyDown event so that when the enter key is pressed, I can handle it myself without affecting the Combobox's default fun ...