node-mongodb-native

Changing mongo database

丶灬走出姿态 提交于 2019-12-21 05:24:09
问题 I want to query a collection in my replica set using the native 2.0 mongodb driver for node. I can connect and authenticated against the admin database but how do I switch databases to query the collection I'm interested in? var mongodb = require('mongodb'); var MongoClient = mongodb.MongoClient; var url = "mongodb://user:pass@db1,db2,db3/admin"; MongoClient.connect(url, function(err, db) { console.log("Connected correctly to server"); console.log("Current database", db.databaseName); //

Defining a map with ObjectId key and array of strings as value in mongoose schema

心已入冬 提交于 2019-12-20 05:35:20
问题 I'm facing a problem while creating Mongoose schema for my DB. I want to create a map with a objectId as key and an array of string values as its value. The closest that I can get is: var schema = new Schema({ map: [{myId: {type:mongoose.Schema.Types.ObjectId, ref: 'MyOtherCollection'}, values: [String]}] }); But somehow this is not working for me. When I perform an update with {upsert: true}, it is not correctly populating the key: value in the map. In fact, I'm not even sure if I have

Synchronous function calls for nodejs mongodb driver

北慕城南 提交于 2019-12-19 10:52:27
问题 I have an open source project that deals with mongodb database. I am trying to make a function that queries the database to check if entry exists. The problem is when if_exists() returning true or false it returns undefined since the mongodb driver function is asynchronous. The file is Query.js and I have tried the solution here to workaround the problem What is the right way to make a synchronous MongoDB query in Node.js? but still I get an undefined result with the get method. What is the

Bluebird Promisfy.each, with for-loops and if-statements?

て烟熏妆下的殇ゞ 提交于 2019-12-18 02:49:47
问题 Right now, the parent for-loop ( m < repliesIDsArray.length ) completes before the first findOne fires, so this all only loops through the last element of the repliesIDsArray..asynchronous.. What's the proper syntax for a promisified version of this codeset? Am new to promisification, and wondering how to start this promisify + loop through arrays + account for if-statements.. Bluebird is required, and Promise.promisifyAll(require("mongoose")); is called. for(var m=0; m<repliesIDsArray.length

MongoDB NodeJS driver's browserify compatibility (debugging help)

拜拜、爱过 提交于 2019-12-14 03:22:04
问题 Trying to figure out what's the problem that I can't browserify mongodb native nodejs driver. I'm assuming there is something wrong here in my setup as I tried googling if there was any known current compatibility problem with MongoDB NodeJS driver and browserify but nothing conclusive came up after couple days trying ;( After running my code on browserify I get a TypeError: require(...).native is not a function I read on the driver's github repo that this driver depends on some native

mongoclient.connection does not return cursor back on command line

半腔热情 提交于 2019-12-14 02:55:00
问题 This is my test1.js console.log("foo"); When I run the test1.js, I got the command line back $ node test2.js foo $ This is my test2.js, using MongoDbClient var MongoClient = require('mongodb').MongoClient; MongoClient.connect("mongodb://local.host/test?w=1", function(err, db) { console.log("foo"); }); However when I run test2.js, I have to press CTRL-C to get the command line back $ node test3.js foo ^C $ What's the difference? and What should I do to get the command line back(close

Equivalent to mongo shell db.collection.runCommand() in Node.js

久未见 提交于 2019-12-13 18:13:16
问题 I'd like to use full text search available in MongoDB 2.4. Text search is available through runCommand function e.g. db.collection.runCommand( "text", { search: "keywords"}) . So, I'm wondering whether there is an equivalent to runCommand() function in mongojs or node-mongodb-native modules. I know the question has been touched before but was never answered sufficiently. Thanks in advance. 回答1: I found that as an equivalent: collection.find({ $text: { $search : "your search words" }})

How to buffer MongoDB inserts during disconnect in node.js?

人盡茶涼 提交于 2019-12-13 12:53:54
问题 We do read an XML file (using xml-stream) with about 500k elements and do insert them into MongoDB like this: xml.on(`endElement: product`, writeDataToDb.bind(this, "product")); Insert in writeDataToDb(type, obj) looks like this: collection.insertOne(obj, {w: 1, wtimeout: 15000}).catch((e) => { }); Now when the Mongo connection gets disconnected, the xml stream still reads and the console gets flooded with error messages (can't insert, disconnected, EPIPE broken, ...). In the docs it says:

GridFS: Clean out all unreferenced files

北慕城南 提交于 2019-12-12 19:24:21
问题 I have just moved towards storing things in my GridFS in MongoDB. During testing, I noticed many files are being created but not deleted properly. I have a collection users , which has a field avatar . It contains the ObjectId of the file. Now I'd like to have some command I could use to remove all the files and chunks that are not referenced there. Is it possible to do that with one query? Maybe 'map-reduce'? Also I am not sure how to properly delete GridFS-Files in node-mongodb-native

Mongoose: How to populate 2 level deep population without populating fields of first level? in mongodb

泪湿孤枕 提交于 2019-12-12 09:58:41
问题 Here is my Mongoose Schema: var SchemaA = new Schema({ field1: String, ....... fieldB : { type: Schema.Types.ObjectId, ref: 'SchemaB' } }); var SchemaB = new Schema({ field1: String, ....... fieldC : { type: Schema.Types.ObjectId, ref: 'SchemaC' } }); var SchemaC = new Schema({ field1: String, ....... ....... ....... }); While i access schemaA using find query, i want to have fields/property of SchemaA along with SchemaB and SchemaC in the same way as we apply join operation in SQL database.