Ways to delete a document from Mongodb depends on the existence of a more recent document

Is there a way to automatically delete previous database insertions in my meteor web app whenever a new document is inserted? I've attempted the following code, but it hasn't been successful:

if(SearchLobby.find({profile: Meteor.userId()}).count() > 1){

  var date1=SearchLobby.find().limit(1).sort({$natural:-1}).createdAt;

  if(SearchLobby.find({profile: Meteor.userId()}).createdAt != date1)
    SearchLobby.remove({profile: Meteor.userId()});
}

I'm unsure of alternative approaches to tackle this issue. My initial attempt didn't yield the expected results.

Answer №1

If I were to choose, I'd recommend using Meteor Collection Hooks specifically with the before.insert hook. To implement this on the server side, you can use the following code:

SearchLobby.before.insert(function(userId, doc) {
    SearchLobby.remove({profile:Meteor.userId()});
});

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

How can AngularJS focus on the last element using ng-repeat and md-focus?

, there is a code snippet that utilizes AngularJS ng-repeat directive to iterate through 'items' and 'md-autofocus' attribute to focus on the last element. The code is contained within an md-dialog with vertical scrolling functionality. ...

Performing a series of synchronous JavaScript calls in Node.js by executing GET requests within a for loop

I'm utilizing super-agent in node to interact with a public API and I'm curious if there's a way to make synchronous calls using it. Although I appreciate its automatic caching feature, my research online suggests that synchronous calls are ...

The IIS URL rewrite is causing issues with the rewriting of CSS and JS files

Struggling with my URL rewrites - every time I set up a rewrite for a page, it ends up affecting the CSS and JS files linked within the webpage, resulting in them not displaying properly. In an attempt to fix this issue, I tried using fully qualified path ...

Update the content of a bootstrap modal dialog following a successful upload

As part of a new service, I am modifying data in a database using a bootstrap modal dialog. However, I'm facing an issue where the name of a recently uploaded file is not appearing in the modal dialog body until I close and reopen it. Is there a way t ...

Enabling real-time notifications through Express 4 middleware with socket.io integration

I am in the process of developing a real-time notification system utilizing socket.io. Here is the current server-side code I have implemented: bin/www: var app = require('../app'); var server = http.createServer(app); var io = app.io io.attac ...

Accessing Data from Nested PHP Array in Javascript

My current situation is this: I have a MYSQL database that consists of two fields: 'ID' and 'string'. The 'string' field stores serialized arrays. To extract the data back, I utilize the following PHP code: $result = mysql_q ...

Migrating to Angular Universal for server-side rendering with an external API server

Thank you for taking the time to read my query. I have developed a project using server-side Node.js and client-side Angular 6. The purpose of the app is to provide information on cryptocurrency prices and details. I am now looking to transition my Angu ...

Is it possible to achieve dynamic updating in Angular without utilizing ng-repeat? (with the use of Firebase)

Is there a way to dynamically update the DOM without utilizing ng-repeat in your template? It appears that when using ng-repeat to load a list of objects, any additions or deletions from the database automatically reflect in the DOM. However, if I simply u ...

Remove an item from an array in JavaScript by specifying its value, with compatibility for IE8

Is there a way to remove an item from an array by its value rather than index, while ensuring compatibility with IE8? Any assistance would be greatly appreciated. Thank you. Below is the array in question: var myArray = ['one', 'two', ...

Creating a compilation of material-ui using browserify

I am currently in the process of incorporating material-ui (material-ui.com) JavaScript into my project to utilize its React components. To streamline this process, I have been utilizing browserify to consolidate all the JavaScript files into a single one ...

What is the best way to create a multiple options query using the MongoDB driver in Node.js?

I'm currently facing an issue with a MongoDB query in Node.js that I can't seem to solve. Let's say we have numerous documents in Mongo, each containing an array of tags. tags: [tag1, tag2, tag3] The front-end will be sending parameters a ...

Utilizing the same WebDriverJS instance repeatedly

As a beginner in Selenium, I successfully launched a website using the following Node.js code snippet: var webdriver = require('selenium-webdriver'); var driver = new webdriver.Builder() .forBrowser('chrome') .build(); console ...

Encountering the error "Cannot GET /login" while attempting to send a file through a post request in Express.js

I'm having trouble sending a new HTML file to the user after a successful login. Every time I attempt to send the file, I keep getting an error message saying "Cannot GET /login" on the page. Below is the section of code that's causing me diffic ...

Trouble with the JavaScript split function

I am trying to split the data in a specific way. Given a string value like this var str = "[:en]tomato[:ml]തക്കാളി[:]"; I am aiming for the following output: tomato,തക്കാളി. I attempted to achieve this using the code sni ...

Looking to prevent printing and saving a webpage directly from the file menu using JQUERY or JavaScript

I am in need of assistance to find a code that can disable the ability to print and save a webpage using JQUERY or JAVASCRIPT ...

Is it possible to assign a property name using a string during the creation of an object?

Can an object property be named directly within the object declaration instead of afterwards? For example, this syntax works: var name = "foo"; var obj = {}; obj[name] = "bar"; // obj.foo === "bar" But is there a way to define it inside the object it ...

Utilizing React Typescript Discriminating Unions to choose between two different types based solely on props

In my project, I have a component that consists of different types: type Base = { color: string } type Button = { to: string } & Base type Link = { link: string linkNewTab: boolean } & Base type ComponentProps = Button | Link e ...

Struggling to adjust the dimensions of a React Barcode to the specified width and height?

I've encountered an issue with resizing the React Barcode Image. I prefer all barcodes to have a width of 200px and a height of 100px. From what I understand, the width of the barcode will expand when the length value increases. Below is my current co ...

What are the benefits of keeping JavaScript and HTML separate in web development?

Recently, I came across the concept of Unobtrusive JavaScript while researching best practices in the realm of JavaScript. One particular point that grabbed my attention was the idea of separating functionality (the "behavior layer") from a web page's ...

Connect to Node-Red websocket server

My server is running node-red with embedded functionality. I am attempting to set up a new websocket listener on the server, but when I run the code provided, the websockets in my node-red application stop functioning properly. const WebSocket = require(& ...