I have a date in my document. How do I query for all documents that are no more than 7 days old. I cannot assume that the time on the requestor machine and the database are in sync.
You can get the server datetime using "serverStatus" command:
In mongo shell:
server_time = db.adminCommand("serverStatus")['localTime'].getTime();
db.mycollection.find( { "change_date": { $gt: new Date((server_time) - 7 * 24 * 60 * 60 * 1000) }} )
In Java:
MongoClient mongoClient = new MongoClient();
MongoDatabase database = mongoClient.getDatabase("dbname");
Document serverStatus = database.runCommand(new Document("serverStatus", 1));
Instant server_time = (Instant) serverStatus.get("localTime");
Related
I'm using the following extension:
https://code.visualstudio.com/docs/azure/mongodb
To perform queries and light data transformation against a Mongo database. I'm having trouble working out how to issue a find request which matches an ObjectId.
I tried:
db.Epochs.find({
'ModelId': '624616797870316ac1432d52'
}).sort({'End': -1})
This results in an empty result set (this ID definitely exists because I copied that value from Compass.
I tried:
db.Epochs.find({
'ModelId': ObjectId'624616797870316ac1432d52')
}).sort({'End': -1})
Which results in the following error:
Unexpected token, expected "," (15:23) 13 | 14 | db.Epochs.find({ > 15 | 'ModelId': ObjectId('624616797870316ac1432d52') | ^ 16 | }).sort({'End': -1}) 17 | 18 | //'EndLogs._impl': { '$exists': true}
I tried adding the NodeJS driver setup calls like:
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');
Which errors with:
Cannot find module 'mongodb' Require stack: - c:\Users\Ian\.vscode\extensions\mongodb.mongodb-vscode-0.9.2\dist\languageServerWorker.js
Finally I tried:
db.Epochs.find({
'ModelId': { '$oid': '624616797870316ac1432d52' }
}).sort({'End': -1})
Which errors with:
unknown operator: $oid
I am currently connecting to mongodb atlas using the following code
exports.connectToMongoose = async() =>{
try{
const result = await mongoose.connect(process.env.mongodbURL,{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex:true,
useFindAndModify:false
})
}
const connectionResult = await mongooseConnection.connectToMongoose();
Although I am the only one user logged In . I can see that mongodb atlas is using 25 connections from the 500 available. Why does each client uses 25 connections and does that mean the mongodb atlas can only handle 20 concurrent clients
db.serverStatus().connections This may be helpful to get the total connections from your clients to your server or primary node in your cluster
Are you sure thats from your one connection? Each Mongo server will have multiple connections from other servers in the cluster, such as Mongos, and Arbiters, as well as health checks and monitoring.
Found this a while back that gives a snapshot of the connections and the IPs they came from.. if this helps
db.currentOp(true).inprog.reduce((accumulator, connection) => {
ipaddress = connection.client ? connection.client.split(":")[0] : "Internal";
if(typeof accumulator[ipaddress] == "undefined"){
accumulator[ipaddress]= {"active":0, "inactive":0};
}
if(connection.active==true) {
accumulator[ipaddress]["active"] = (accumulator[ipaddress]["active"] || 0) + 1;
accumulator["ACTIVE"] = (accumulator["ACTIVE"] || 0 ) +1;
} else {
accumulator[ipaddress]["inactive"] = (accumulator[ipaddress]["inactive"] || 0) + 1;
accumulator["INACTIVE"] = (accumulator["INACTIVE"] || 0 ) +1;
}
accumulator["TOTAL_CONNECTION_COUNT"]++;
return accumulator;
},
{ TOTAL_CONNECTION_COUNT: 0 }
)
consider an ISO8601 date
and want to do a query using Typeorm and PostgreSQL
if (orderInput.begining && orderInput.ending)
query.andWhere(`order.createdAt
BETWEEN to_timestamp(${orderInput.begining} / 1000 )
AND to_timestamp(${orderInput.ending} / 1000 );
`);
These are my argument:
"2010-12-24T21:32:33.477Z"
"2019-12-24T21:32:33.477Z"
and this is the underhood query and the err :
query failed: SELECT DISTINCT "distinctAlias"."order_id" as "ids_order_id" FROM (SELECT "order"."id" AS "order_id", "order"."createdAt" AS "order_createdAt"
FROM "order" "order" INNER JOIN "ware" "ware" ON "ware"."id"="order"."wareId" WHERE "order"."organizationId" = $1 AND "order"."createdAt"
BETWEEN Wed Dec 25 2019 01:02:33 GMT+0330 (Iran Standard Time)
AND Wed Dec 25 2019 01:02:33 GMT+0330 (Iran Standard Time) ;) "distinctAlias" ORDER BY "order_id" ASC LIMIT 25 -- PARAMETERS: ["8fd87ced-eb58-4460-b74e-d5a2b1491622"]
error: { error: syntax error at or near "Dec"
I guess it's because of that the arguments are not taken wraped in ' ' (qoutes)
I don't know how to pass the arguments to typescript as standard ISO8606 Date(typescript) with ' '(qoutes) to be then passed to PostgreSQL
My solution as of now(2021-05-26) using Between of TypeORM (docs):
const orders = await this.orderRepo.find({
where: {
createdAt: Between(
new Date(orderInput.begining).toISOString(),
new Date(orderInput.ending)).toISOString(),
),
},
skip: skip || 0,
take: take || 10,
});
Both Typescript And PostgreSQL know ISO8601 well and there is no need to to_timestamp() for PostgreSQL.
So this works like a chram:
if (orderInput.begining && orderInput.ending)
query.andWhere(
`"order"."createdAt"
BETWEEN :begin
AND :end`
,{ begin: orderInput.begining, end: orderInput.ending);
NOTE: in JavaScript, you can easily make a ISO8601 standard Date in this way:
const date = new Date(2020,2,2)
const iso = date.toISOString()
Try this:
await this.YOUR_REPOSITORY
.createQueryBuilder("order")
.where('order.createdAt BETWEEN :startDate AND :endDate', { startDate: orderInput.begining, endDate: orderInput.ending })
.getMany();
I'm trying to create a connection and add a document with mongoengine through an SSH tunnel.
A successful attempt with pymongo can be seen below, I simply want something similar with mongoengine. :-)
from auth import *
import pymongo
from sshtunnel import SSHTunnelForwarder
server = SSHTunnelForwarder(
(HOST_IP, HOST_PORT),
ssh_username = SSH_USER,
ssh_password = SSH_PASS,
remote_bind_address = ('localhost', 27017)
)
server.start()
client = pymongo.MongoClient('127.0.0.1', server.local_bind_port)
db = client[MONGO_DB]
db.authenticate(MONGO_USER, MONGO_PASS)
coll = db.queue_db
coll.insert({"testFile42":43})
server.stop()
mongoengine.connect(
db=DB_NAME,
host="127.0.0.1",
port=server.local_bind_port
)
I am using mongo-java-driver-2.9.1 for interacting with mongodb, I want to log the query that are fired on to the mongodb server. e.g. In java for inserting the document this is the code that I write
DBCollection coll = db.getCollection("mycollection");
BasicDBObject doc = new BasicDBObject("name", "MongoDB")
.append("type", "database")
.append("count", 1);
coll.insert(doc);
for this, equivalent code in "mongo" client for inserting document in mongodb is
db.mycollection.insert({
"name" : "MongoDB",
"type" : "database",
"count" : 1
})
I want to log this second code, is there any way to do it?
I think the MongoDB Java driver has not logging support so you have to write your logging Message by your own. Here an Example:
DBCollection coll = db.getCollection("mycollection");
BasicDBObject doc = new BasicDBObject("name", "MongoDB")
.append("type", "database")
.append("count", 1);
WriteResult insert = coll.insert(doc);
String msg = "";
if(insert.getError() == null){
msg = "insert into: " + collection.toString() +" ; Object " + q.toString());
//log the message
} else {
msg = "ERROR by insert into: " + collection.toString() +" ; Object " + q.toString());
msg = msg + " Error message: " + insert.getError();
}
//log the message