MongoDB

134

Solutions

251

Contributors

MongoDB (from humongous) is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas. MongoDB is developed by MongoDB Inc. and is free and open-source, published under a combination of the GNU Affero General Public License and the Apache License.

Share tech news, updates, or what's on your mind.

Sign up to Post

Connection Error: No Mongos proxy available.

Environment: Node.js, MongoDB, Mongoose

Start.js
const mongoose = require('mongoose');


mongoose.connect(process.env.DATABASE,  { useNewUrlParser: true })
mongoose.Promise = global.Promise;
mongoose.connection
  .on('connected', () => {
    console.log(`Mongoose connection open on ${process.env.DATABASE}`);
  })
  .on('error', (err) => {
    console.log(`Connection error: ${err.message}`);
  });

.env file

DATABASE=mongodb://username:DBpassword@player-shard-00-00-1cmur.mongodb.net:27017,player-shard-00-01-1cmur.mongodb.net:27017,player-shard-00-02-1cmur.mongodb.net:27017
0
Powerful Yet Easy-to-Use Network Monitoring
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

I want to write a init script to stop and start the MongoDB.
0
{
 "_id" : "Sh",

"Name" : "HR",
"Form" : [
    {
        "Name" : "HR",
        "Permission" : {},
        "Fields" : [
            {
                "OutputFormat" : "Text",
                "Validation" : [],
                "Name" : "PASS",
                "Permission" : {
                    "Hidden" : [
                        "ac"
                    ]
                },
                "IsFormulaArg" : false,
                "MaxCharacters" : 100.0,
                "Label" : "PASS",
                "Widget" : "Dropdown",
                "DefaultPermission" : [],
                "Dropdown" : "me",
                "Score" : 1,
                "PermissionType" : "Hidden",
                "Id" : "me"
            },


            {
                "OutputFormat" : "dd/MM/yyyy",
                "NodeList" : {
                    "Score" : 2,
                    "Type" : "CellReference",
                    "Id" : "jdk2",
                    "Value" : null,
                    "CellMetadata" : {
                        "72uw" : "CreatedAt"
                    }
                },
                "Name" : "Date_Prepared",
                "Permission" : {},
                "IsFormulaArg" : false,
                "Required" : true,
                "Widget" : "Date",
                "Label" : "Date Prepared",
                "Score" : 2,
                "FormulaStr" : "CreatedAt",
                **"Formula" : "True",**
      …
0
So i have a user schema like this:

var user_schema = new Schema({
   username:{type:String,required: true, unique : true, trim: true},
   college:{type:String,required: true},
   password:{type:String,required: true, trim: true},
   email:{type:String,required: true, unique : true, trim: true},
   phone:{type:Number,required: true, unique : true, trim: true},
   dp:{type:String},
   tag:{type:String},
   description:{type:String},
   friends:[{type:String}],
   pending:[{type:String}],
   skills:{type:String},
   bucket:[{type:String}]
  });

Open in new window

and my objective is, to search the all the documents in the collection to get people based on the following conditions:

1. They should not be in the users' "friends" array.
2. They should not be in the users' "pending" array.
3. They should have the same "tag" (a string value) as the user.

So, basically I have to compare the users' fields ("friends","pending" and "tags"), with fields of all documents in the whole collection.

How do I do it, using mongoose (nodejs mongodb library)
0
I have to initialize an array to [](empty array) and then push user chose "tags" into it. This is my schema:

 var user_schema = new Schema({
   username:{type:String,required: true, unique : true, trim: true},
   college:{type:String,required: true},
   password:{type:String,required: true, trim: true},
   email:{type:String,required: true, unique : true, trim: true},
   phone:{type:Number,required: true, unique : true, trim: true},
   dp:{type:String},
   tags:[{type:String}],
   description:{type:String},
   skills:{type:String},
   bucket:[{type:String}]
 }); 

Open in new window

` and i have to initialize "tags" to [] and push user string values (user entered/chosen) every time the request is made to the API call.

I am using this line of code:

 stu_user.findOneAndUpdate(
   { _id: req.decoded.id },
   { $set:{tags:[]},$push: { tags: 'some chosen string'  } },
  function (error, success) {
         if (error) {
            console.log(error);
        } else {
            console.log(success);
        }
    });

Open in new window

But it's not working.
0
How to import collections from mongodb from cli?
0
Hey, This is just a decision-making problem. I am seeking for a well thought out answer.
I have to develop an android XMPP chat application, (the application also has a NodeJS server API, connecting to MongoDB and AWS S3 for picture uploads)

which will be better:

1. Having a "openfire" server on aws and connecting it to the android application and implementing an XMPP client on the android device using "smack" library.

2. Implementing a "xmpp-client" on NodeJS server side and scrape the results from this API to the Android device.
0
In MongoDb collection a DateTime field is stored in MST dateTime format for over 1 millions of record for a older record and now new records in utc dateTime format,
  Is there any cost effective query to update the date property in collections for those old recordsin MST dateTime
  (update the registered date format from MST to UTC format i.e. update time + 7:00 hr of each of these record)? We have MongoDb 3.3 version.
0
I'm having difficulty putting a complex(to me) aggregation query together for my data. Basically, here's the run down so everyone understands my conundrum.

I have two collections in this equation. "training_documents" and "users". Each user object has a key identified by name "trainings" which is an array of objects. Each object within this trainings array, contains 4 key/value pairs. An example of each object is below.

    {
    	"document": ObjectId('5a0350ad7df0977d94cffab6'),
    	"trainee": ObjectId('59e51a4b7df0977d94cff95d'),
    	"trainer": ObjectId('595fcc2e04cf707693257890'),
    	"completion_time": "2018-04-23T21:28:22.747Z"
    }

Open in new window


The users trainings array will contain many objects following the aforementioned format.

An example user data structure is below for reference.

   
 [
    	{
    		"_id": "5ad782283c55b056bcc39e3z",
    		"site": {
    			"site_id": "site1",
    			"site_name": "Site One"
    		},
    		"user_name": "jsmith",
    		"first_name": "John",
    		"middle_name": "A",
    		"last_name": "Smith",
    		"full_name": "John Smith",
    		"title": "Duh Boss...",
    		"email": "testuser@example.com",
    		"last_login": "2018-04-27T14:27:27.014Z",
    		"type": "full-time",
    		"active": true,
    		"__v": 0,
    		"badge_id": "000001780343232123",
    		"trainings": [
    			{
    				"document": ObjectId('5ae33622a766885a121b7362'),
    				"trainee": 

Open in new window

0
nodeJs and MongoDB
I Would like to  Create a dynamic navbar with mean stack a navbar that the admin can modify it navbar has
Main Categoy
Sub Categories title
sub category
example image https://prnt.sc/j3nxz7
0
Simplify Active Directory Administration
LVL 8
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Mongo 2.6.12

Not a mongo admin just trying to figure out an issue no one else wants to take on.

Mongo is flushing logs after 24 hours, where is that configuration set or better yet what command can I run against a collection to see the details of log retention?
0
Hi There,

I have a mongodb aggregate query in PHP that is throwing an error and I can't see what I've missed in the syntax.

Versions are
mongodb =  3.6
PHP  = 7.1
mongo PHP driver = 1.3.1-1


 The query in mongo shell works and looks like this:

db.products.aggregate(
	[
		{
			$match: {
			    download_Date : {'$gte' : 20180205 }
			}},
		{
			$sort: {
			download_Date: 1 }
		},
		{
			$group: {
			 _id: "$cw_product_id", batch: { $last : "$download_Date" }
	        }},
	]

Open in new window


My code in PHP fails and  looks like

$command = new MongoDB\Driver\Command([
    'aggregate' => 'products',
    'pipeline' => [    
        ['$match' =>  [ 'download_Date' => ['$gte' => $batch ]]],
        ['$sort'  => [  
            'download_Date' => 1
        ]],
    ['$group' => [ 
        '_id' => [ '$cw_product_id', 'batch' => ['$last' => '$download_Date'] ],
        ]],   
    ],
        'allowDiskUse' => true, 
        'cursor' => new stdClass, 
    ]);

Open in new window


The error I receive when running the php script is

           
PHP Fatal error:  Uncaught MongoDB\Driver\Exception\RuntimeException: Unrecognized expression '$last'

Open in new window

I've pulled it apart and put it back together so many times, a fresh pair of eyes is needed to spot the obvious.  

Thanks in advance,

Rick
0
Hi,

I have a text file which has multiple json rows in it.

What I need to do is the following:-

1. Read thru the file
2. Select only certain elements not all, remap to a new name for example AuthenticationId could be AuthId
3. Ingest in to MongoDB as JSON

How can this be achieved in Python?

{"AuthenticationId":"997","CommandLine":"C:\\Windows\\system32\\wbem\\wmiprvse.exe -secured -Embedding","ConfigBuild":"1007.3.0005907.1","ConfigStateHash":"3163607488","EffectiveTransmissionClass":"3","Entitlements":"15","ImageFileName":"\\Device\\HarddiskVolume3\\Windows\\System32\\wbem\\WmiPrvSE.exe","ImageSubsystem":"2","IntegrityLevel":"16384","MD5HashData":"1df2fc82b861bc9612657d1661e9ae33","ParentAuthenticationId":"997","ParentProcessId":"1627804482161","ProcessCreateFlags":"16","ProcessEndTime":"","ProcessParameterFlags":"24577","ProcessStartTime":"1512962460","ProcessSxsFlags":"64","RawProcessId":"9276","SHA1HashData":"1aa3fda50123dd14a055b4d6601beedead69fe11","SHA256HashData":"835f2a94e47830b06654e484bf7a1cc0b9882f579716dca198e32d22218a07e5","SourceProcessId":"1627804482161","SourceThreadId":"39913451443946","TargetProcessId":"1756187997441","TokenType":"2","UserSid":"S-1-5-19","aid":"2bc82f8878df4b9f7712273e755a93be","aip":"105.255.135.108","cid":"99cdff8f89af458d858c3d6b3e312e11","event_platform":"Win","event_simpleName":"ProcessRollup2","id":"4ff73eae-de22-11e7-a0dd-06e913674db2","name":"ProcessRollup2V6","timestamp":"1512962460994"}

Open in new window



Open in new window

0
MongoDB v2.6.12 keeps losing my user credentials on a database. I'm running Iquidus Explorer and thus need to be running v2.6.x of MongoDB for it to work. Most of the time it works fine, but every now and again my script disconnects and gets an error that the user credentials are no longer correct.

To fix this I have to run mongo and type use database1 and then db.createUser( { user: "username", pwd: "blahblah", roles: [ "readWrite" ] } )

This is far from ideal. Can anyone shed any light on what's going on and how to prevent the credentials from getting screwed up?

I'm running MongoDB on CentOS 7 64-bit.
0
I've been thinking to migrate my erp(java currently) from rdbms(firebird currently) to nosql (mongoDB likely) and i trying to antecipate some issues. I'm trying to make a "failproof" inventory control which never let a qty's item be smaller than 0. Offcourse i still get "ACID bias" which i need to left in order to complete this task. The goal is update inventory qty of all order's items when approve the order. For that, 2 problems can occur which currently are resolved with a trigger.

1.problem: order gets 2 items (ball = qty 2 and chair = qty 1) and there is no enough qty in inventory (ball = qty 1 and chair = qty 0) to complete the order. Once i trigger the command do complete the order, a loop is executed to decrease qty in inventory and fails on the second item cause there is no enough qty. Consequently rollback operation is trigged and everybody go home happy.

2.problem: in one word, concurrency. suppose there is no trigger controling this operation but only "selects and if". User 1 query inventory qtys, see enough qty (ball = 2, chair = 1) e system allow complete the order. While the transaction is running user 2 query same invetory qtys but because first transaction is not complete yet second query see "old" qty (ball = 2, chair = 1) e system also lets complete order. Consequently, 1 qty of chair is descreased twice, but shold be descreased only once and fail on the second. Result: i got negative qty.

I saw some workarrounds with inventory reservation but i …
0
Created a duplicate username entry in mongoose database and resulted in duplicated error. Now everytime I start server, I get this error:

(node:14839) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CastError: Cast to ObjectId failed for value "{ type: 'Buffer',
  data: [ 89, 243, 30, 231, 66, 69, 45, 56, 123, 65, 153, 72 ] }" at path "_id" for model "users"
(node:14839) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
0
I am trying to update a dict with values searched from mongoDB whose parameters are taken from another dict. What I have till now is:

    lst = {'a':1}
    b={'Name':'name_1', 'Address':'Address_1'}
    lst.update(k:coll.find({"_id":id}{v:1})) for k,v in b.items()

What is the right way to do this procedure?
0
Hi, I have the following document and I want to delete all keys that are empty:

so it should delete the key year from first instance and build on second instance since is empty. I want the query to update all documents and clear that array of options of empty values.

{
    _id: 'sldkfjsldkf',
    name: "Red car",
    options: [
       {
          color: "red",
          year: "",
          build: "2000"
       },
       {
          color: "yellow",
          year: "1988",
          build: ""
       },
  ]

}
0
I just installed ElasticSearch, MongoDB, GrayLog2 but getting an error and unable to access the web interface. Please, suggest any solution r

/etc/elasticsearch
# ls -lrtha
drwxr-x---.  2 root elasticsearch    6 Apr 24 16:29 scripts
-rwxr-x---.  1 root elasticsearch 2.6K Apr 24 16:04 logging.yml
-rwxr-x---.  1 root elasticsearch 3.2K Jul 20 05:05 elasticsearch.yml


/var/log/graylog-server/server.log

2017-07-20T07:50:36.694Z INFO  [CmdLineTool] Loaded plugin: Elastic Beats Input 2.2.3 [org.graylog.plugins.beats.BeatsInputPlugin]
2017-07-20T07:50:36.696Z INFO  [CmdLineTool] Loaded plugin: Collector 2.2.3 [org.graylog.plugins.collector.CollectorPlugin]
2017-07-20T07:50:36.699Z INFO  [CmdLineTool] Loaded plugin: Enterprise Integration Plugin 2.2.3 [org.graylog.plugins.enterprise_integration.EnterpriseIntegrationPlugin]
2017-07-20T07:50:36.700Z INFO  [CmdLineTool] Loaded plugin: MapWidgetPlugin 2.2.3 [org.graylog.plugins.map.MapWidgetPlugin]
2017-07-20T07:50:36.708Z INFO  [CmdLineTool] Loaded plugin: Pipeline Processor Plugin 2.2.3 [org.graylog.plugins.pipelineprocessor.ProcessorPlugin]
2017-07-20T07:50:36.709Z INFO  [CmdLineTool] Loaded plugin: Anonymous Usage Statistics 2.2.3 [org.graylog.plugins.usagestatistics.UsageStatsPlugin]
2017-07-20T07:50:36.813Z ERROR [CmdLineTool] Invalid configuration
com.github.joschi.jadconfig.ValidationException: Cannot read file elasticsearch_config_file at path /etc/elasticsearch/elasticsearch.yml. Please specify the correct …
0
JavaScript Best Practices
LVL 12
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Hi,

Please bear with me, my Mongo knowledge is minimal and my google foo has failed me after an hour or two.

I have data in Mongo that looks as follows:

{
    "_id" : ObjectId("xxxx"),
    "applicationData" : {
        "groups" : [
            "group1",
                  "group2",
        ],
        "status" : "ok"
    },
    "appid" : "UID0001",
}

{
    "_id" : ObjectId("yyyy"),
    "applicationData" : {
        "groups" : [
                  "group2"
        ],
        "status" : "ok"
    },
    "appid" : "UID0001",
}

From this I'm wanting to group by the content of "groups" and the appid an get a count - the result would be as follows:

group1  UID0001 1
group2  UID0001 2

One step further would be that I'd only like it to report where the "count" is greater than 1

How can I achieve this?

I guess, in SQL, it would be:

select groups, appid, count(*) from db having count(*) > 1

I'm not having much (any) success, further complicated with one of the values I'm wanting to group by is in an Array.

Thanks in advance
0
I am using php and mongoDb. I am new to mongoDb, but fine with php.  In my mongoDb database I have a  collection called 'users' with a number of fields such as firstname, lastname etc. There are a number of users.


I also have a field/collection in some users called web_links which contains an array.
ie: within my users document:
{
    "_id" : ObjectId("587af11ec09cf31a1955ed92"),
    "username" : "mike",
    "firstname" : "Mike",
    "lastname" : "Tester",
    "email" : "mike@test.com",
    "web_links" : [
        {
            "name" : "google",
            "link" : "https://google.com",
            "status" : "1",
            "added" : ISODate("1970-01-18T08:00:57.600Z")
        },
        {
            "name" : "yahoo",
            "link" : "https://yahoo.com",
            "status" : "1",
            "added" : ISODate("1970-01-18T08:00:57.600Z")
        }
    ]
}

I am trying to firstly update the status of an array item.
I have got as far as this ->

$name = 'google';
$newstatus = '0';

$this->database->users->updateOne(['_id'=>new MongoDB\BSON\ObjectId($userid)],['web_links.name'=>$name],['$set'=>['web_links.$.status'=>$newstatus]]);

This is returning the following error via firebug:
Uncaught exception 'MongoDB\Exception\InvalidArgumentException' with message 'First
 key in $update argument is not an update operator'.

I have tried to change $set to $push, but that is not the answer.

I would be very grateful for assistance …
0
I have a SP in which i need to add one condition to check multiple databases by using sys.dm_database_encryption_keys table. 

when encryption_state is 2 and percentage_completed is 0 in the results it should print. Below is my query



DECLARE @Test varchar(10)
SELECT @Test = Database_ID from 
                             sys.dm_database_encryption_keys  
WHERE Encryption_state = 2 and complete_percentage = 0
If (@test not NULL)
Begin
PRINT("ALTER DB NAme")

If i have multiple DB's which meets the about. It should PRINT multiple ALTER statements with DB Names
0
My data:

Gage_number Latitude    Longitude   Date    Gage_1  Gage_2  Gage_3

1   35.02   -80.84  1/1/2002    0.23    0   0.7
2   35.03   -81.04  1/2/2002    0   0   0.2
3   35.06   -80.81  1/3/2002    3.2 2.1 0.1
This is just a subset of data. I around 50 gauge stations. I want to find spatial auto correction between my gauge stations for rain fall. Based on distance between them. I have created my distance matrix. But I don’t want to use any library in R. I want to do all steps in a function.

loc <- read.table("rain_data.txt",header=TRUE,fill=TRUE)  
gauge.dists <- as.matrix(dist(cbind(loc$Latitude, loc$Latitude))) #distance matrix
Now since distance between gauges is not uniform. I want to use a certain bin size to decide about distance lags.

Pseudocode:

If the distance between guage pair 1-2 is 1 meter then assign a distance lag of 1 and so on So Lag 1=intergage dist=1 meter. So Lag 5=intergage dist=5 meter After creating that matrix I will find autocorrelation between gauge pairs.

so for lag 1 intergage dist=1 for lag 5 intergage dist=5

Gage pair   date    RainA   RainB       Gage pair   date    RainA   RainB

1-2 1/1/2002    0.23    0       1-3 1/1/2002    0.23    0.7
1-2 1/2/2002    0   0       1-3 1/2/2002    0   0.2
1-2 1/3/2002    3.2 2.1     1-3 1/3/2002    3.2 0.1
I have a hard time translating it into loop or a function. Any ideas?
0
Using Database 11g Release 2

When I run the following pl/sql script :

BEGIN
  FOR comp IN (SELECT val FROM productTable@remote_link2 WHERE PRODUCT_ID = 1111)
  LOOP
    DBMS_OUTPUT.PUT_LINE(comp);
  END LOOP;
END;

I get the error :
ORA-06550: line 2, column 34:
PL/SQL: ORA-00942: table or view does not exist
ORA-06550: line 2, column 16:
PL/SQL: SQL Statement ignored
ORA-06550: line 4, column 26:
PLS-00364: loop index variable 'COMP' use is invalid
ORA-06550: line 4, column 5:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

But when I run as pl/sql only the select statement in the for loop :
SELECT val FROM productTable@remote_link2 WHERE PRODUCT_ID = 1111;
I get the results.

Is the database link not compiling properly in a for loop?
0
Hi,
I am working on the vehicle details normalization but I am not sure it is correct or not. could you check it for me please? -we have: vehicle make, manufacturer, model, colour, type, fuel, year and safety.
I have no idea about year and safety . Thanks for your help.



manufactures(manufactur_id, manufactur_name, manufactur_address, manufactur_website)

vehicle_model(model_id, model_name, manufactur_id(fk))

vehicle_colour(colour_id,colour)

fuel(fuel_id,fuel_type)

type(type_id,type_description)

(year,safety)

vehicle(vehicle_id, colour_id(fk), model_id(fk), fuel_id(fk), type_id(fk), ......)
0

MongoDB

134

Solutions

251

Contributors

MongoDB (from humongous) is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas. MongoDB is developed by MongoDB Inc. and is free and open-source, published under a combination of the GNU Affero General Public License and the Apache License.

Top Experts In
MongoDB
<
Monthly
>