Updating Mongo documents through node but no results - node.js

I've been trying to add emails (which is a new field) to my "people" collection but I don't know why I'm getting no results from the server. This is my code
for (key in D_emails) {
console.log(D_emails[key])
try {
o_id = new mongo.ObjectID(D_emails[key])
collection.updateOne({
_id: o_id
}, {
$set: {
"Email": key
}
}, function(err, results) {
if (err) {
throw err
} else if (results.length) {
console.log(results)
} else {
console.log("no results")
}
});
} catch (e) {
print(e);
}
}

According to mongo documentation for updateOne the response of the query (results in your case) does not contain length element and it is not an array or an object. This will be the response according to the documentation:
Returns a document containing:
A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled
matchedCount containing the number of matched documents
modifiedCount the number of modified documents upsertedId containing the _id for
the upserted document

first of all. try to code without try catch blocks when using the concept of "err" object being returned from every asynchronous callback. You can treat the error right inside the callback. "throwing" the error will also prevent other emails from being updated. But maybe that's what you want. It doesn't really matter now.
Now, back to your question. let me show you what result is, in this example:
// inserting.
collection.insertOne({_id: "lala", a:10, b:20}, function (err, r) {
assert.equal(null, err);
assert.equal(1, r.insertedCount);
// updating.
collection.updateOne({_id: "lala"}, {$set: {a: 99}}, {}, function(err, result) {
assert.equal(null, err);
console.log(result) // first thing printed.
console.log(JSON.stringify(result, null, "\t")) // second print.
// checking whats inside it.
collection.findOne({_id: "lala"}, {}, function(err, doc) {
assert.equal(null, err);
console.log(doc) // third print.
db.close() // don't close connection if you don't need to.
})
})
})
the 3 printed things will be (scroll to the end to see what you actually want):
CommandResult {
result: { ok: 1, nModified: 1, n: 1 },
connection:
Connection {
domain: null,
_events:
{ close: [Object],
error: [Object],
timeout: [Object],
parseError: [Object],
connect: [Function] },
_eventsCount: 5,
_maxListeners: undefined,
options:
{ socketOptions: {},
auto_reconnect: true,
host: 'localhost',
port: 27017,
cursorFactory: [Object],
reconnect: true,
emitError: true,
size: 5,
disconnectHandler: [Object],
bson: BSON {},
messageHandler: [Function],
wireProtocolHandler: [Object] },
id: 1,
logger: Logger { className: 'Connection' },
bson: BSON {},
tag: undefined,
messageHandler: [Function],
maxBsonMessageSize: 67108864,
port: 27017,
host: 'localhost',
keepAlive: true,
keepAliveInitialDelay: 0,
noDelay: true,
connectionTimeout: 0,
socketTimeout: 0,
destroyed: false,
domainSocket: false,
singleBufferSerializtion: true,
serializationFunction: 'toBinUnified',
ca: null,
cert: null,
key: null,
passphrase: null,
ssl: false,
rejectUnauthorized: false,
checkServerIdentity: true,
responseOptions: { promoteLongs: true },
flushing: false,
queue: [],
connection:
Socket {
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'localhost',
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_eventsCount: 8,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 334,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1,
read: [Function],
_consuming: true },
writeStream: null,
hashedName: '29bafad3b32b11dc7ce934204952515ea5984b3c',
buffer: null,
sizeOfMessage: 0,
bytesRead: 0,
stubBuffer: null },
matchedCount: 1,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0 }
{
"ok": 1,
"nModified": 1,
"n": 1
}
{ _id: 'lala', a: 99, b: 20 }
the first object is the result returned from "updateOne()".
the second object is the result's "toString()" implementation and this is what's inside result.result (scroll back to top).
the last object is what I get after querying for the updated document.
you can find more examples on the native mongodb driver for nodejs website: http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#updateOne
their are full of examples.

Related

how to find database sessionids for each runstream method api call while executing query in gcloud spanner?

we are seeing database sessions more than the max connections configured.
database.runStream( queryOptions)
below was the session configuration :
const SESSIONCONFIG = {
max: 10,
min: 1,
keepAlive: 10
};
How can we print sessionid using database.runStream api call to ensure same sessionid was reused?
created database connection, using session config options. session pool displayed with below details:
'
pool_:
SessionPool {
_events:
[Object: null prototype] { error: [Function: bound emit], close: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
isOpen: true,
database: [Circular],
options:
{ acquireTimeout: Infinity,
concurrency: Infinity,
fail: false,
idlesAfter: 10,
keepAlive: '1',
labels: [Object],
max: '5',
maxIdle: 1,
min: '1',
writes: 0 },
_myDatabaseConnection: { readonly: [Array], readwrite: [], borrowed: Set {} },
_requests:
PQueue {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
_carryoverConcurrencyCount: false,
_isIntervalIgnored: true,
_intervalCount: 1,
_intervalCap: Infinity,
_interval: 0,
_intervalId: null,
_intervalEnd: 0,
_timeoutId: null,
queue: [PriorityQueue],
_queueClass: [Function: PriorityQueue],
_pendingCount: 0,
_concurrency: Infinity,
_isPaused: false,
_resolveEmpty: [Function],
_resolveIdle: [Function] },
_acquires:
PQueue {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
_carryoverConcurrencyCount: false,
_isIntervalIgnored: true,
_intervalCount: 1,
_intervalCap: Infinity,
_interval: 0,
_intervalId: null,
_intervalEnd: 0,
_timeoutId: null,
queue: [PriorityQueue],
_queueClass: [Function: PriorityQueue],
_pendingCount: 0,
_concurrency: 1,
_isPaused: false,
_resolveEmpty: [Function],
_resolveIdle: [Function] },
_traces: Map {},
_onClose: Promise { <pending> },
_evictHandle:
Timeout {
_called: false,
_idleTimeout: 600000,
_idlePrev: null,
_idleNext: null,
_idleStart: 1644,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: 600000,
_destroyed: false,
_handle: [Timer],
[Symbol(unrefed)]: false,
[Symbol(asyncId)]: 76,
[Symbol(triggerId)]: 1 },
_pingHandle:
Timeout {
_called: false,
_idleTimeout: 60000,
_idlePrev: null,
_idleNext: null,
_idleStart: 1644,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: 60000,
_destroyed: false,
_handle: [Timer],
[Symbol(unrefed)]: false,
[Symbol(asyncId)]: 78,
[Symbol(triggerId)]: 1 } } }'
return new Promise((resolve, reject) => {
database.runStream(queryOptions)
.on('error', (error) => {
// how to ensure same session id object being used
logger.error( error);
reject(error);
})
.on('data', (row) => {
allData.push(row.toJSON());
})
.on('end', () => {
if (allData.length === 1) {
resolve(allData[0]);
}
});
});
Expected: return session to pool once query execution complete without closing.
Current: Every time a new session created and total number of sessions created exceeds maximum allocated.
I'm not yet sure whether it's possible to get the session ID used by the runStream query. But something you can try is to call Database.getSessions and print the IDs of all sessions. Then you can check at each call to runStream whether new sessions are added or if the total number of sessions exceeds the maximum that you've configured.
How are you checking that it's creating more than the configured max number of sessions?
Looking at the runStream implementation, for each query execution, it is taking a read session from the configured session pool. The pool will not create any more sessions after it's full.

How to read password protected PDF file in Nodejs and get it in buffer?

I tried using pdfjs-dist.
getting large json response.
var PDFJS=require('pdfjs-dist');
PDFJS.getDocument({ url: 'p1.pdf', password: '' }).then(function(pdf_doc)
{
console.log(pdf_doc);
}).catch(function(error) {
// incorrect password
// error is an object having 3 properties : name, message & code
});
Response
This is the whole response I am getting.
but I need response in buffer.
Can it be converted to buffer.
PDFDocumentProxy {
loadingTask:
{ _capability:
{ resolve: [Function], reject: [Function], promise: [Promise] },
_transport:
WorkerTransport {
messageHandler: [Object],
loadingTask: [Circular],
commonObjs: [Object],
fontLoader: [GenericFontLoader],
_params: [Object],
CMapReaderFactory: [DOMCMapReaderFactory],
destroyed: false,
destroyCapability: null,
_passwordCapability: null,
_networkStream: [PDFNodeStream],
_fullReader: [PDFNodeStreamFsFullReader],
_lastProgress: [Object],
pageCache: [],
pagePromises: [],
downloadInfoCapability: [Object],
numPages: 4,
pdfDocument: [Circular] },
_worker:
{ name: null,
destroyed: false,
postMessageTransfers: true,
verbosity: 1,
_readyCapability: [Object],
_port: [LoopbackPort],
_webWorker: null,
_messageHandler: [Object] },
docId: 'd0',
destroyed: false,
onPassword: null,
onProgress: null,
onUnsupportedFeature: null },
_pdfInfo:
{ numPages: 4,
fingerprint: '3432353738363537336c6e665361446f6f744f4a70' },
_transport:
WorkerTransport {
messageHandler:
{ sourceName: 'd0',
targetName: 'd0_worker',
comObj: [LoopbackPort],
callbackId: 1,
streamId: 1,
postMessageTransfers: true,
streamSinks: [Object],
streamControllers: [Object: null prototype] {},
callbacksCapabilities: [Object: null prototype] {},
actionHandler: [Object],
_onComObjOnMessage: [Function] },
loadingTask:
{ _capability: [Object],
_transport: [Circular],
_worker: [Object],
docId: 'd0',
destroyed: false,
onPassword: null,
onProgress: null,
onUnsupportedFeature: null },
commonObjs: { objs: [Object: null prototype] {} },
fontLoader:
GenericFontLoader {
docId: 'd0',
nativeFontFaces: [],
styleElement: null,
loadingContext: [Object],
loadTestFontId: 0 },
_params:
[Object: null prototype] {
url: 'p1.pdf',
password: '',
rangeChunkSize: 65536,
CMapReaderFactory: [Function: DOMCMapReaderFactory],
ignoreErrors: true,
pdfBug: false,
nativeImageDecoderSupport: 'none',
maxImageSize: -1,
isEvalSupported: true,
disableFontFace: true,
disableRange: false,
disableStream: false,
disableAutoFetch: false,
disableCreateObjectURL: false },
CMapReaderFactory: DOMCMapReaderFactory { baseUrl: null, isCompressed: false },
destroyed: false,
destroyCapability: null,
_passwordCapability: null,
_networkStream:
PDFNodeStream {
source: [Object],
url: [Url],
isHttp: false,
isFsUrl: true,
httpHeaders: {},
_fullRequest: [PDFNodeStreamFsFullReader],
_rangeRequestReaders: [Array] },
_fullReader:
PDFNodeStreamFsFullReader {
_url: [Url],
_done: false,
_storedError: null,
onProgress: [Function],
_contentLength: 112979,
_loaded: 112979,
_filename: null,
_disableRange: false,
_rangeChunkSize: 65536,
_isStreamingSupported: true,
_isRangeSupported: true,
_readableStream: [ReadStream],
_readCapability: [Object],
_headersCapability: [Object] },
_lastProgress: { loaded: 112979, total: 112979 },
pageCache: [],
pagePromises: [],
downloadInfoCapability:
{ resolve: [Function], reject: [Function], promise: [Promise] },
numPages: 4,
pdfDocument: [Circular] } }
*ignore below text*
efwrg rgsretg resgerstgh;ergh ;resjgysregh regjes powrjgu oiuueryoeq uieqroeqreqrilih ehr oiyeroeq ioiyeqroeq oieyqrioeq oieqyr oiyeqr oiyeqrp ioqyet oiehr oiyerh oieyreq oiyheqri iohereqk ioheqr qerioyqereq ioehqriheq rioqehriqeb ioeqrhpeq ioeqrhiqe ioqehriq ioqerhioq oirhqeipor oiqehrieq ioehqrq ioeqhrieq iohqerpq ieqhrpeq ioeqhrpeq iheqrpqe oiehrpqe ieqhrqierh ioeqhr ieqhr ioeqrh piqerh ieqhr iheqr piheqr ioheqr iheqr ioeqhrp ioqhre oieqhr oeqiyr qoeiryf pouqer poqure pouqr pouqre[q poquerq poqeur[q poqeur poqwuer poquer[ poqwur[wq poqr[ poqwhr powrq pow
You may open and read a password protected PDF like below. Working with your existing code:
var PDFJS = require('pdfjs-dist');
PDFJS.getDocument({ url: 'p1.pdf', password: '' }).then(function(pdf)
{
let text = [];
for(let i = 1; i <= pdf.numPages; i++) {
pdf.getPage(i).then(function(page) {
page.getTextContent().then(function(data) {
for(let j = 0; j < data.items.length; j++) {
text.push(data.items[j].str);
}
});
});
}
}).catch(function(error) {
// incorrect password
// error is an object having 3 properties : name, message & code
});

Rethinkdb query always returns null Eventhough data is existed?

I am new to rethinkDB,I am trying to find data by username using filter function.
But rethinkDB returns null eventhough data existed.
//Define Your Api//Define Your Api
import express from 'express';
import r from 'rethinkdb';
const router = express.Router();
router.post('/users',(req,res)=>{
let username = req.body.data.username;
let password = req.body.data.password;
console.log(username,password);
r.connect({db:"image"}).then((conn) => {
r.table("users").filter({username:"arfo"}).run(conn,function (err, data) {
console.log(data) //null
})
})
});
export default router
Updated
it returns me a bunch of data like this do i have to manipulate this data
Cursor {
_type: 3,
_eachCb: [Function],
_conn:
TcpConnection {
host: 'localhost',
port: 28015,
db: 'image',
authKey: '',
timeout: 20,
ssl: false,
outstandingCallbacks: {},
nextToken: 2,
open: true,
closing: false,
buffer: <Buffer >,
_events: {},
_eventsCount: NaN,
_closePromise: null,
rawSocket:
Socket {
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'localhost',
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_eventsCount: 7,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 325,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
user: 'admin',
password: '',
read: [Function],
_consuming: true } },
_token: 1,
_opts: {},
_root: { [Function] args: [ [Object], [Object] ], optargs: {} },
_responses: [ { t: 2, r: [Object], n: [] } ],
_responseIndex: 0,
_outstandingRequests: 0,
_iterations: 0,
_endFlag: true,
_contFlag: false,
_closeAsap: false,
_cont: null,
_cbQueue: [],
_closeCb: null,
_closeCbPromise: null,
next: [Function] }
From the RethinkDB docs, it looks like run returns (err, data). For example (from the docs):
r.table('marvel').run(conn, function(err, cursor) {
cursor.each(console.log);
})
So if you update your code to:
r.table("users").filter({username:"arfo"}).run(conn,function (err, data) {
console.log(data)
})
Then it should remove the null log that you were seeing.
I'm no RethinkDB expert, but from the docs it looks like if you want to get the data from the response then you can call toArray on the cursor:
r.table("test").run( conn, function(error, cursor) {
cursor.toArray( function(error, results) {
console.log(results) // results is an array of documents
})
})

MongoDb find() returns a lot of unwanted fields

app.get('/',function(req,res){
var response={};
db.collection('first').find({},function(err,result){
if(err){
db.close();
console.log('Error');
console.log(err);
}
else{
db.close();
console.log('Success');
console.log(response);
}
});
res.end(response);
});
However, this returns a lot of unwanted data. I looked through that data too, but I do not see the entry values/fields made in the collection. I want it to return field values only as a JSON object. How should I do this ?
Sample output: (Too much to post everything)
Readable {
connection: null,
server: null,
disconnectHandler:
{ s: { storedOps: [], storeOptions: [Object], topology: [Object] },
length: [Getter] },
bson: {},
ns: '****',
cmd:
{ find: '******',
limit: 0,
skip: 0,
query: {},
slaveOk: true,
readPreference: { preference: 'primary', tags: undefined, options: undefined } },
options:
{ skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: { preference: 'primary', tags: undefined, options: undefined },
db:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter],
options: [Getter],
native_parser: [Getter],
slaveOk: [Getter],
writeConcern: [Getter] },
promiseLibrary: [Function: Promise],
disconnectHandler: { s: [Object], length: [Getter] } },
topology:
EventEmitter {
domain: null,
_events:
{ reconnect: [Function],
timeout: [Object],
error: [Object],
close: [Function],
destroy: [Object] },
_eventsCount: 5,
_maxListeners: undefined,
and more......
Thank you.
First of all, you're logging and sending back an empty response object. Your issue come from the syntax, find() takes only one parameter and returns a cursor, you have to call next() on it inside a loop to get every document or call toArray() to get all documents at once:
app.get('/', function (req, res) {
db.collection('first').find({}).toArray(function (err, docs) {
db.close();
if (err) {
console.log('Error');
console.log(err);
res.end();
}
else {
console.log('Success');
console.log(docs);
res.json(docs);
}
});
});

What's proper way to insert a document using node API for MongoDB?

I'm using node-mongo for querying and inserting documents to a mongoDB database.
This is what I'm assuming for inserting:
collection.insert({}, function(err,insertedDocuments){
});
However for insertedDocuments I expect them to be the actual documents, but this is what insertedDocuments is always looking like:
{"ok":1,"n":1}
The important code to explain what I'm doing is:
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
MongoClient.connect('mongodb://127.0.0.1:27017/places', function (err, db) {
if (err) throw err;
console.log("Connected to database ... Will work with default places collection".inverse);
places = db.collection('places');
strings = db.collection('strings');
app.route('/places').post(newPlaceController);
app.listen(6190, function() {
console.log('Express listening'.inverse);
});
});
function newPlaceController (request,response) {
console.log(request.body);
var latitude = request.body.lat;
var longitude = request.body.long;
var name = request.body.name;
var newPlace = getNewPlaceObject(name,latitude,longitude); // This returns the object/document to be inserted
places.insert(newPlace, function (err,createdPlace) {
if (err)
response.send(JSON.stringify(err),500);
else {
console.log(createdPlace);
response.send(JSON.stringify(createdPlace),200);
}
});
}
Strangely, the log of createdPlace looks like this:
{ result: { ok: 1, n: 1 },
connection:
{ domain: null,
_events:
{ close: [Object],
error: [Object],
timeout: [Object],
parseError: [Object],
connect: [Function] },
_maxListeners: 10,
options:
{ socketOptions: {},
auto_reconnect: true,
host: '127.0.0.1',
port: 27017,
cursorFactory: [Object],
reconnect: true,
emitError: true,
size: 5,
disconnectHandler: [Object],
bson: {},
messageHandler: [Function],
wireProtocolHandler: {} },
id: 2,
logger: { className: 'Connection' },
bson: {},
tag: undefined,
messageHandler: [Function],
maxBsonMessageSize: 67108864,
port: 27017,
host: '127.0.0.1',
keepAlive: true,
keepAliveInitialDelay: 0,
noDelay: true,
connectionTimeout: 0,
socketTimeout: 0,
domainSocket: false,
singleBufferSerializtion: true,
serializationFunction: 'toBinUnified',
ca: null,
cert: null,
key: null,
passphrase: null,
ssl: false,
rejectUnauthorized: false,
responseOptions: { promoteLongs: true },
flushing: false,
queue: [],
connection:
{ _connecting: false,
_handle: [Object],
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_maxListeners: 10,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
onend: null,
destroyed: false,
bytesRead: 56,
_bytesDispatched: 215,
_pendingData: null,
_pendingEncoding: '',
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1,
pipe: [Function],
addListener: [Function: addListener],
on: [Function: addListener],
pause: [Function],
resume: [Function],
read: [Function],
_consuming: true },
writeStream: null,
buffer: null,
sizeOfMessage: 0,
bytesRead: 0,
stubBuffer: null },
ops: [ { name: 'OXXO', loc: [Object], _id: 553723a2a2c10c273605309a } ] }
What am I doing wrong?
The new record is at createdPlace.ops[0]. What you are calling createdPlace is not the new document, but a wrapper object the API docs call result. It contains metadata about the operation and then under the ops property array you can find the new documents.
Here's an excerpt from the documentation
The insert command will return a results object that contains several fields that might be useful.
result Contains the result document from MongoDB
ops Contains the documents inserted with added _id fields
connection Contains the connection used to perform the insert
You might consider a more-convenient module such as monk which will return the document to you. (Also recommended because in general the mongodb native API is so bad it's practically developer-hostile).

Resources