I have a simple Tags Collection in Meteor. Currently in order to ensure that a user cannot create a duplicate Tag document I do this:
var existingTag = Tags.findOne({name: "userInput"})
If existingTag is undefined then I can go ahead and do the insert.
Is there a better/correct way of doing this utilizing meteor mongodb syntax? Cant seem to find any documentation on this.
Thanks.
A good solution is to create Mongo index at the unique field. That way you'll have the uniqueness validation at Mongo level, as well as performance increase for searches on that field.
Meteor currently doesn't support index creation directly, so you need to manually log in to your database and add your index from there. The command for this is:
db.tags.ensureIndex({name: 1}, {unique: true})
Here and here you can find more information.
Related
I was wondering if I can maintain a straight clone of my main database, including values.
That clone database would be used for testing porpuses, with the data of the development database.
I've searched the sequelize docs and didn't find any command or technique that solves that problem. Is there a way to do that, or a way around it?
Maybe a way to dump the database into the new one, or something like that.
Seeders would be way too laborious.
I use mariadb.
In my node app, I am saving data in to dynamic mongodb collections and those collections are named after a value in the data.
Sample code:
function saveToDb(data){
let collectionName = data.someType;
let collection = getCollection(collectionName);
return collection.save(data);
}
Please note that I do not know the collection name upfront, and the collections are being created as and when the corresponding data come for the first time.
If i want to index a few known fields in this document, I am thinking of calling collection.createIndex() after every save. But, will it hit the performance too much? Or any better ways to do this?
Finally went ahead with calling collection.createIndex() after every save. Monitoring it for a few weeks now and finding no performance problem identified with ~25K records.
Note: This doesnt mean calling collection.createIndex() after every save is the ideal solution. If someone has a better one, please feel free to comment.
To insert in node into a Mongo Collection the command is
db.collection.insertOne({.........});
and currently my collection would a string inputted by the user
db.$$<VarCollectionName>.insertOne({........]);
Doing this causes my program to crash. What is the correct syntax to go about doing this? The end goal is to create a new collection everytime the user uses my method and parse a text file into it.
To insert in node into a Mongo Collection the command is:
db.collection.insertOne({.........});
It's not, actually, at least not when you're using the official driver, where the syntax would be:
db.collection(NAME).insertOne(...);
So NAME can be a user-provided variable (although from a security standpoint, you should probably validate if the user is allowed to write to that collection).
This is not really related to MongoDB, just the usual JavaScript.
db[variableHoldingTheCollectionName].insertOne({...});
I'm trying to think of the best way to import csv data into rethinkdb while avoiding possible duplicates (eg importing the same file twice).
The csv data comes from bank statements. There is no real primary key, instead a composite key of date,description and amount can be used.
I wanted to use the CLI for imports but I couldn't see how I could do that given I don't have a single primary key.
Is the best way to iterate through the csv, first check for the existence and then insert if not found? I struggled to create a single query that would insert only if the check for existence was empty.
Any guidance - I was assuming this is a somewhat common scenario?
(I'm using JavaScript as my language)
Thanks in advance!
You could set the conflict options on the insert statement to either "update" or "replace" This will check to see if a document with the same specicifed id already exists and either update or replace it. http://www.rethinkdb.com/api/javascript/insert/
r.table("users").insert(
{id: "william", email: "william#rethinkdb.com"},
{conflict: "replace"}
).run(conn, callback)
I'm writing a Meteor (Node.js) app which uses MongoDB on the backend. At a certain point in my code, I need to update a specific document within a collection. I need to use Mongo's update() method, but I'm having trouble passing in the proper (complex) query to narrow down to that one, specific document. The document I'm trying to update is:
db.collection.find({market: 'AAA'}).sort({creationDate:-1}).limit(1)
In words, the one document in collection that has a market of AAA and was created most recently (creationDate is a UNIX timestamp number, e.g. 1408829429914). There are multiple documents with a market of AAA, so I am using sort() and limit(1) to find the document which was created most recently.
Mongo's update() doesn't seem to accept sorting parameters as part of the query before the update process. What can I do to narrow down to this document and update it? Thanks!
You will need to fetch the document you want and then update it by _id. If your collection was called Markets, the code would look like:
var market = Markets.findOne({market: 'AAA'}, {sort: {creationDate:-1}});
Markets.update(market._id, {$set: {fancy: true}});
It's worth mentioning that even if MongoDB supported the optimization you are looking for, meteor client code can only update by _id anyway.