not accessing old data on mongodb atlas by Node application - javascript

I have not used mongodb atlas for 10 days.
when I am trying to connect node application after 10 days into mongodb atlas returning blank array in find Query from Node application , when I inserted new data into mongodb atlas using Node application then find Query return only new data .
Problem is that mongodb atlus not returning old data.
but App collection have
db.apps.count({}) > 29
Code
let mongoose = require("mongoose");
let uri = mongodb atlus path || "mongodb://localhost:27017/test";
mongoose.connect(uri, function (err, client) {
console.log('mongoose connect SuccessFully');});
let appSchema = new mongoose.Schema({name:{type: String}});
let taxSchema = new mongoose.Schema({tax:{type: String}});
var App = mongoose.model('App', appSchema);
var Tax = mongoose.model('Tax', taxSchema);
App.find({}).then(function (apps) {
console.log(apps); // returning [] "
});
Tax.find({}).then(function (tax) {
console.log(tax); // returning []
});
facing problem with old data of mongodb atlas not giving.
Every Query returns null or array depend upon what Query you have . I am stuck from past one day with mongodb atlas.
this code work fine with local mongodb .

Your old data might be in a different database.
By default, you are connecting to the test database.
To see the different databases and collections, the easiest way is:
Open the "Cluster" section of Atlas console
Click on "collections".
You will see different databases on the cluster and the collections
and data inside them.
See if you can find your old data in one of
those

For me the problem was that I didn't properly update the new mongodb+srv//*** connection string I got from Atlas "connect menu".
Was trying to connect to:
mongodb+srv://user:pwd#host/test?retryWrites=true&w=majority
But needed to use following:
mongodb+srv://user:pwd#host/MY_DATABASE_NAME?retryWrites=true&w=majority

Related

How can I get the id of a inserted document firebase-admin

I am handling some documents with firebase realtime database. I need to delete a document that I don't have the access to read it client side via SDK. In order to do that I need to know what is the id, so I should store it in my db (mongo) and when I need to delete a document on firebase I just get it from my DB and the I delete it.
I took a look to this answer but it doesn't work for me.
I insert documents into my firebase DB with this method (server side using firebase-admin)
const writeNotification = (uid: string, notification: INotification) => {
const db = admin.database();
const notsRef = db.ref(`notifications/${uid}`);
notsRef.push({
..._.omit(notification, 'to'),
});
};
If I do notsRef.id I get undefined.
How can I get the ID of my document that I have just inserted?
p.s. my documents are organized like this:
The answer you are looking at is about Firestore and not Realtime Database that you are using. The Reference has a key property and not id:
console.log('New Key', notsRef.key)
// to get child node's key
const childNotesRef = notsRef.push({
..._.omit(notification, 'to'),
});
console.log(childNotesRef.key)

Error when creating a new document inside a new subcollection in firestore

I am using the v9 Web SDK.
I have a collection called 'transactions-in' which keeps a record of every transaction into a cardano wallet.
From time to time my server will send a transaction in response to each transaction in this collection (such as sending an nft or refunding ADA) and I want to add these transactions into a subcollection on the relevant transaction document, so the structure looks like this:
collection: 'payments_in'
document: document ID
subcollection: 'related_transactions'
document: newly created document reference
This is the code I have to get the right path to the newly created document ref and upload some test data into the firebase emulator:
//1. Get current doc
const currentDoc = doc(db, 'payments_in', transactionId);
//2. Build refund database entry
//2A. Get collection from database (will create one if doesn't exist)
const newCollection = collection(currentDoc, 'related_transactions');
//2B. Get a new document from database within new collection
const newSubDoc = doc(newCollection);
//2C. Upload entry to database
await setDoc(newSubDoc, { test: 'test' });
When I log the path of newSubDoc I get this, so the path looks good (I think??): payments_in/kXK5pimm29nRhU4CohA6/related_transactions/NL11eYmsB4M9vHw6PI8X
However when I try to submit the entry using setDoc I get the following error message:
#firebase/firestore: Firestore (9.1.1): Connection GRPC stream error.
Code: 3 Message: 3 INVALID_ARGUMENT: Document name "projects//databases/(default)/documents/payments_in/kXK5pimm29nRhU4CohA6/related_transactions/tLoCiHUJaA2uYhxt7drb"
lacks a project id at index 9.
Does anyone see what I am doing wrong here?
Thanks
If you look at the path in the error message, it is missing a project ID after projects/: projects//databases/(default)/documents/payments_in/kXK5pimm29nRhU4CohA6/related_transactions/tLoCiHUJaA2uYhxt7drb. You might want to double check how you initialize db and whether your Firebase configuration is complete.

Connecting my React App to a MySQL Database on a PHP Server

I am new to working with data bases. I have a react app that I would like to connect to a MySQL database. The server is not mine but given by a web provider (strato), using PHP.
Is it generally possible to GET and PUT data via react (meaning javascript) to this data base in a way that I normally work with ajax and a restful API?
I really hope this question provides enough information for an answer :)
Here's a code example from https://github.com/numtel/reactive-mysql-example, file index.js. Have a look at that file and settings.js to see how to set up the connection.
var results = liveDb.select('select * from players order by score desc', [ {
  table: 'players'
} ]).on('update', function(diff, data){
  var msg = JSON.stringify({
    type: 'diff',
    data: diff
  });
  // Send change to all clients
  connected.forEach(function(conn){
    conn.write(msg);
  });
});

How to prevent RethinkDB from creating test database

When you launch a rethinkdb instance, it will automatically create a database called 'test'. When you run multiple instances and cluster them using rethinkdb proxy this leads to the issue:
Database name conflict: test is the name of more than one database
If you try to delete the databases i.e using
r.dbDrop('test').run(conn, function(result) {
console.log(result) // Will result in error
});
This will produce the following error:
ReqlOpFailedError: Database 'test' is ambiguous; there are multiple databases with that name in r.dbDrop("test")
So how to prevent RethinkDB from creating the 'test' database automatically? Or how to remove a database if you run into name conflict?
If you use rethinkdb create to create a data directory, no test database will be created.
For example, if rethinkdb_data doesn't exist, this will create it with no test database:
rethinkdb create
rethinkdb
This will do the same, but uses -d to specify the location of the data directory:
rethinkdb create -d /var/lib/rethinkdb/data
rethinkdb -d /var/lib/rethinkdb/data
After digging for a while I didn't find any good options to prevent RethinkDB from trying to create the default test database on startup. The problem above only occurs when the cluster nodes use different data directories. Otherwise they will not try to create extra test databases as the other nodes will simply recognise it already exists (created by the first node that launched).
I ended up solving this in my backend software by enumerating all database named test during startup from the db_config table in rethinkdb database.
Example:
// Read from the database 'rethinkdb' that holds information about other databases
r.db("rethinkdb")
// Table db_config contains database id, name information
.table("db_config")
// Filter by name 'test'
.filter({name: "test"})
.run(conn, function(err, cursor) {
// Get results from cursor by id and rename
cursor.each(function(err, result){
r.db("rethinkdb")
.get(result.id)
.update({name: "other_name"})
.run(conn, function(err, result) {
console.log(result.replaced);
});
});
});

Node.js and mongodb access mongodb

I'm trying to set up mongodb on Windows 8 using node.js, Does anyone know why im getting this error. C:\users\phill\node_modules\mongodb\lib\mongodb\mongo_client.js:359 it also says at collection = db collection,,, can't call method 'collection' of null. I'm having a hard time setting it up. My goal is to be able to add to mongo db, and see that I add or pull up what I added, but adding something is good enough for me for now. I'm trying every thing I can find, even straight from the website, I tried everything I see on here as well. Think it maybe it's the way I have things set up. My node.js is saved in my c: drive there is a file that says, program files(86x) in there I have node_modules, npm and such. The path ends up being, computer > windows (C:) > program files(86x) > nodejs. My Mongodb is saved right on my C: drive the path end up being windows (C:) > mongodb-win32-x86_64-2008plus-2.4.8. In my C: I also created a file data and in it created another db. I have been told i should just use mongoose, I'm just learning so i open to any advice, links or anything that will help. I have one last question as well, i learned php and then found out about sql injections and stuff like that, i am not seeing anything about security at all, should i expect the same as well. For this i get text not defined, but i have been getting errors with everthing i have done, best i did was get stuck on a right concern screen.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, db) {
test.equal(null, err);
test.ok(db != null);
db.collection("replicaset_mongo_client_collection").update({a:1},
{b:1}, {upsert:true}, function(err, result) {
test.equal(null, err);
test.equal(1, result);
db.close();
test.done();
});
});
Tried this as well and getting a error,C:\users\phill\node_modules\mongodb\lib\mongodb\mongo_client.js:359.... at collection = db collection,,, can't call method 'collection' of null. im calling it in command prompt node filename.js I'm saving it where my node.js file is, I have pulled up files before and created a server.
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');
var db = new Db('test', new Server('localhost', 27017));
// Fetch a collection to insert document into
db.open(function(err, db) {
var collection = db.collection("simple_document_insert_collection_no_safe");
// Insert a single document
collection.insert({hello:'world_no_safe'});
// Wait for a second before finishing up, to ensure we have written the item to disk
setTimeout(function() {
// Fetch the document
collection.findOne({hello:'world_no_safe'}, function(err, item) {
assert.equal(null, err);
assert.equal('world_no_safe', item.hello);
db.close();
})
}, 100);
});
In your first code example, you said:
For this i get text not defined
I assume you meant "test not defined?" Your script only requires the mongodb library, and I don't believe test is a core nodejs function, so that would explain the error.
To reference the driver documentation for db.collection(), an assert library is used, but also properly imported (as you did in your second example).
Within your callback to db.open(), you don't check if an error occurred. That might shed some light on why db is null in that function.
Regarding your question about the equivalent of SQL injection with MongoDB, the main areas of concern are places where you might pass untrusted input into evaluated JavaScript, or using such input to construct free-form query objects (not simply using a string, but more like dropping an object into your BSON query). Both of these links should provide more information on the subject:
What type of attacks can be used vs MongoDB?
How does MongoDB address SQL or Query injection?

Categories

Resources