3

I want to query a MongoDB and console.log some data. At this point, any data is fine. The records in the MongoDB have a lot of properties, but to keep this simple, I'm only referencing one; and I don't know if that is causing a problem. Also, my collection is named 'data', not 'datas', so I'm trying to force Mongo to use 'data'. Cluster: stocks, collection: data.

I realize how simple a query should be, but I haven't managed to print anything from this db with node. Prior to this, I've been using python to populate the db.

var mongoose = require('mongoose');
var url = 'mongodb://<user>:<password>@stocks-shard-00-00-rtpdz.mongodb.net:27017,stocks-shard-00-01-rtpdz.mongodb.net:27017,stocks-shard-00-02-rtpdz.mongodb.net:27017/test?ssl=true&replicaSet=stocks-shard-0&authSource=admin'

mongoose.connect(url, { useMongoClient: true })

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
  var stockSchema = mongoose.Schema({
      Symbol: String
  });

  var data = mongoose.model('data', stockSchema);

  data.find(function (err, stocks) {
    if (err) return console.error(err);
    console.log(stocks);
  })
});

This returns:

[]

This is an example record, queried with python:

{u'Date': datetime.datetime(2017, 12, 22, 0, 0),
 u'Symbol': u'ADSK',
 u'_52w_high': u'-20.35%',
 u'_52w_low': u'41.88%',
 u'_52w_range': u'73.60 - 131.10',
 u'_id': ObjectId('5a3d22b24a579477f9f7ce91'),
 u'analystrec': u'2.00',
 u'atr': u'2.99',
 u'avg_vol': u'2.15M',
 u'beta': u'1.92',
 u'bookvalpershare': u'0.49',
 u'cashpershare': u'6.62',
 u'change': u'-0.01%',
 u'currratio': u'1.10',
 u'debt_to_eqty': u'14.78',
 u'dividend': u'-',
 u'earnings': u'Nov 28 AMC',
 u'employees': u'9000',
 u'eps': u'-2.57',
 u'eps_growth_nxt_y': u'1.22',
 u'eps_nxt_5y': u'26.00%',
 u'eps_nxt_q': u'-0.12',
 u'eps_nxt_yr': u'1.22',
 u'eps_past_5y': u'-32.80%',
 u'eps_qq': u'15.10%',
 u'eps_this_yr': u'-78.70%',
 u'finalAnswer': 2,
 u'finalQuote': 0,
 u'forwpe': u'85.32',
 u'gross_margin': u'84.20%',
 u'income': u'-566.80M',
 u'insider_trans': u'-28.35%',
 u'insiders_own': u'0.30%',
 u'inst_own': u'98.10%',
 u'inst_trans': u'1.13%',
 u'lt_debt_to_eqty': u'14.78',
 u'marketcap': u'22.93B',
 u'oper_margin': u'-24.90%',
 u'optionable': u'Yes',
 u'payout': u'-',
 u'pb': u'213.12',
 u'pc': u'15.77',
 u'pe': u'-',
 u'peg': u'-',
 u'perf_half_yr': u'-3.08%',
 u'perf_m': u'-18.23%',
 u'perf_q': u'-6.58%',
 u'perf_wk': u'-1.71%',
 u'perf_yr': u'38.08%',
 u'perf_ytd': u'41.10%',
 u'pfcf': u'-',
 u'prev_close': u'104.43',
 u'price': u'104.42',
 u'profit_margin': u'-18.23%',
 u'ps': u'11.57',
 u'quickratio': u'1.10',
 u'rel_vol': u'0.84',
 u'roa': u'-12.80%',
 u'roe': u'-136.20%',
 u'roi': u'-25.10%',
 u'rsi_14': u'31.00',
 u'sales': u'1.98B',
 u'sales_past_5y': u'-1.70%',
 u'sales_qq': u'5.20%',
 u'short_float': u'2.40%',
 u'short_ratio': u'2.45',
 u'shortable': u'Yes',
 u'shs_float': u'219.48M',
 u'shs_outst': u'219.60M',
 u'sma20': u'-5.51%',
 u'sma200': u'-2.34%',
 u'sma50': u'-11.46%',
 u'target_price': u'132.00',
 u'volatility': u'2.07% 2.67%',
 u'volume': u'212,785'}
Randy
  • 105
  • 1
  • 1
  • 9

1 Answers1

2
  1. Set useMongoClient option, while trying to connect mongodb using mongoose:

    mongoose.connect(url, { useMongoClient: true })

  2. The default mongodb collection names are plural, so this is a reason why your collection name is datas instead of data

kaxi1993
  • 4,535
  • 4
  • 29
  • 47
  • 1. Fixed the error, thank you. 2. Yes, it's the default, but I've read it can be forced. I need to force it. – Randy Dec 24 '17 at 19:55
  • I implemented the answer from here to force 'data' collection: https://stackoverflow.com/questions/10547118/why-does-mongoose-always-add-an-s-to-the-end-of-my-collection-name ...but it still returns: [ ] – Randy Dec 24 '17 at 20:07
  • Are you sure that there are any records in the database in data collection? – kaxi1993 Dec 24 '17 at 20:25
  • Yes, I'm sure there are records because I query it daily with python. I double-checked with a query just now also. – Randy Dec 24 '17 at 20:51
  • In python, I connect with the same link, but then I also have to specify the database like this: db=client.stocks . "stocks" is in the link, but maybe I need to tell this javascript explicitly to use "stocks". – Randy Dec 24 '17 at 21:04
  • Can you share the last part of mongodb_url you use. – kaxi1993 Dec 24 '17 at 21:33
  • I put data into a 'datas' collection, and it still returns [ ] – Randy Dec 24 '17 at 22:45
  • 'mongodb://:@stocks-shard-00-00-rtpdz.mongodb.net:27017,stocks-shard-00-01-rtpdz.mongodb.net:27017,stocks-shard-00-02-rtpdz.mongodb.net:27017/test?ssl=true&replicaSet=stocks-shard-0&authSource=admin' – Randy Dec 24 '17 at 22:50
  • Solved! The problem was with the link. The database can be put into the link near the end where it says "test": '...mongodb.net:27017/te‌​st?ssl=true&replicaS‌​et=stocks-shard-0&au‌​thSource=admin' ... When I changed "test" to "stocks", it worked. – Randy Dec 25 '17 at 00:38
  • Great! Congrats Randy – kaxi1993 Dec 25 '17 at 05:06