These are chat archives for Automattic/mongoose

20th
Mar 2018
Kev
@lineus
Mar 20 2018 00:01
@saeedhei gist here
Sedric Heidarizarei
@saeedhei
Mar 20 2018 00:10
@lineus I mean when I have const iMsg ={msg: '2000',} my update work well
But when I get iMsg from a chat user , I got that error
I want to get all string {} from a user and put in $set
Sedric Heidarizarei
@saeedhei
Mar 20 2018 06:16
I Think maybe my incoming iMsg is String {msg: '2000'} and Should change to JSON object
Sedric Heidarizarei
@saeedhei
Mar 20 2018 06:35
I used JSON.parse(iMsg); but I got SyntaxError: Unexpected token m in JSON at position 6
Sedric Heidarizarei
@saeedhei
Mar 20 2018 07:25
✅ Solved with This: const obj = eval("(" + iMsg + ")"); link(https://stackoverflow.com/questions/13718326/javascript-string-to-object)
Sedric Heidarizarei
@saeedhei
Mar 20 2018 07:36
Can I use This Code another way? Because I have a warning: [eslint] eval can be harmful
Kev
@lineus
Mar 20 2018 08:47
@saeedhei I would strongly advise against the use of eval with user input. It's dangerous.
you can use String.replace() with a regex to get at the contents.
Kev
@lineus
Mar 20 2018 09:01
> userInput = "{ msg: 'this is from a user' }"
'{ msg: \'this is from a user\' }'
> inputString = userInput.replace(/(^[^']+')(.+)(' [}]$)/, '$2')
'this is from a user'
>
Kev
@lineus
Mar 20 2018 09:10
regexes can be taken advantage of too though, you need to defend against malicious input either way.
Bruno Barros
@bybrunobarros
Mar 20 2018 10:04
@lineus Hi! I saw you gist (https://gist.github.com/lineus/233f1637c069aada0b4e2554b8cf7298), thanks. Yeah I'd prefer to only populate the relevant testimony. I don't know if it's the right way, but I'm still focused on getting only the required data :)
Kev
@lineus
Mar 20 2018 10:07
@bybrunobarros I think you'll have to find the 'featured' testimony with a query against the db instead of the virtual instead then, since the virtual doesn't get propagated to the db.
Kev
@lineus
Mar 20 2018 10:22
@bybrunobarros I'm interested in how you end up solving this. If you're up for sharing the solution you land on, let me know ;)
Kev
@lineus
Mar 20 2018 11:01

@bybrunobarros I was playing around with this, but I have to go get my kids to school

  Org.aggregate([ { $match: { _id: mong._id } } ])
    .unwind('testimonies')
    .match({ 'testimonies.state': { $in: ['featured'] } })
    .lookup({
      from: 'people',
      localField: 'testimonies.person',
      foreignField: '_id',
      as: 'testimonyUser'
    })
    .exec((err, doc) => {
      if (err) { return console.error(err) }
      console.log(doc[0])
      return mongoose.connection.close()
    })

output:

gitter: ./testimonies.js 
{ _id: 5ab0e9afae6a2b3c4742c141,
  name: 'MongoDB',
  testimonies: 
   { _id: 5ab0e9afae6a2b3c4742c143,
     body: 'This is so cool!',
     state: 'featured',
     person: 5ab0e9afae6a2b3c4742c13f },
  __v: 0,
  testimonyUser: [ { _id: 5ab0e9afae6a2b3c4742c13f, name: 'Zachary', __v: 0 } ] }
gitter:

it needs some refinement, but it can be done in one query :)

Sedric Heidarizarei
@saeedhei
Mar 20 2018 12:15

@lineus Should My Code be like This ?

const inputString = iMsg.replace(/(^[^']+')(.+)(' [}]$)/, '$2');
await Board.update({_id: '5aafad719c19482ad4cf6a00'}, {$set: {msg: inputString}}, {upsert: true}, (err) => {

How Should I get for 3 msg ?

   " {
      msg1: "2000",
      msg2: "3000",
      msg3: "4000"
    }"
Kev
@lineus
Mar 20 2018 12:18
is that last code block really an object literal? or is that a string?
' { msg1: "2000", msg2: "3000", msg3: "4000"  }'
and do you have control over the code that generates the value of iMsg? or are you consuming data from a 3rd party?
Sedric Heidarizarei
@saeedhei
Mar 20 2018 12:19
my iMsg is dynamic and seted from admin user, admin will set msg1 and 2 and 3 , after set i will want to get
Bruno Barros
@bybrunobarros
Mar 20 2018 12:20
@lineus Iike your idea of aggreagation
Kev
@lineus
Mar 20 2018 12:21
I'm still playing with it, there are a lot of tools there :)
Kev
@lineus
Mar 20 2018 12:28
@saeedhei do you have control over the creation of the data that eventually becomes the value of iMsg?
Sedric Heidarizarei
@saeedhei
Mar 20 2018 12:31
I can make key in my code but user set the value, how should I get 3 value of msg1 and 2 and 3 invidual?
inputString and inputString2 and inputString3
I mean 3 regex and 3 value
Kev
@lineus
Mar 20 2018 12:32
I don't mean the values of the msg's in the string ' { msg1: "2000", msg2: "3000", msg3: "4000" }' I mean the entire string itself.
Sedric Heidarizarei
@saeedhei
Mar 20 2018 12:32
yes user enter all of that string ' { msg1: "2000", msg2: "3000", msg3: "4000" }'
and I Should Get 3 value and put them in $set

@lineus I can tell to user write this too

msg1: "2000"
msg2: "3000"
msg3: "4000"

But I Don't know How Should I get the Value?
or this

msg1: 2000
msg2: 3000
msg3: 4000
Kev
@lineus
Mar 20 2018 12:38
bascially, you should either be using JSON.stringify to pack that data on the user side so that you can use JSON.parse on the server side or if that's not an option, you can use this regex to handle multiple msgs in a single string:
> str = '{ msg1: "2000", msg2: "3000", msg3: "4000" }'
'{ msg1: "2000", msg2: "3000", msg3: "4000" }'
> JSON.parse(str.replace(/([\w]+):/g, '\"$1\":'))
{ msg1: '2000', msg2: '3000', msg3: '4000' }
the keys have to be quoted in the input to JSON.parse, so the regex just handles that and makes the str parseable.
Sedric Heidarizarei
@saeedhei
Mar 20 2018 12:57
What is This mean '\"$1\":
Kev
@lineus
Mar 20 2018 12:58
it substitues the first parenthesis match in the regex with quotes around that match and a colon
> str = '{ msg1: "2000", msg2: "3000", msg3: "4000" }'
'{ msg1: "2000", msg2: "3000", msg3: "4000" }'
> str.replace(/([\w]+):/g, '\"$1\":')
'{ "msg1": "2000", "msg2": "3000", "msg3": "4000" }'
>
this is in the node repl by the way, in case you haven't used it before. It's a great little tool for testing/demonstrating ideas like this, and is really useful for honing things like regexes :)
Sedric Heidarizarei
@saeedhei
Mar 20 2018 12:59
Thank you, worked like a charm.
Kev
@lineus
Mar 20 2018 13:00
glad I could help!
Sedric Heidarizarei
@saeedhei
Mar 20 2018 21:56
When Should I close mongoose connection? Should I Close that for CURD? if I have 2 update function in 2 pages, Should I Close 2 times?
Angel Daniel Munoz Gonzalez
@AngelMunoz
Mar 20 2018 22:13
hey guys sorry wasn't able to answer
let me put the schemas
const locationSchema = new Schema({
  country: { type: String },
  state: { type: String, unique: true },
  city: String,
});

const industrySchema = new Schema({
  name: { type: String, unique: true },
});

const companySchema = new Schema({
  name: { type: String },
  globalId: String,
  source: String,
  logoUrl: String,
  companyIds: [{ type: String }],
  industries: [industrySchema],
  location: locationSchema,
});
Angel Daniel Munoz Gonzalez
@AngelMunoz
Mar 20 2018 22:19
and the rest of the info is in this issue
Automattic/mongoose#6248
Kev
@lineus
Mar 20 2018 22:53
@saeedhei best practice afaik is to use as few connection as possible. I only close the connection when I know my app is about to finish or die.
@saeedhei but I do have a script that runs every hour 24/7 kinda like a cron job that closes and opens the connection each time. If it works, and doesn't cause extra cost or time debugging issues, I say go with it!
@AngelMunoz thanks for the great question. I tried the best I could to understand what you were trying to do.
Kev
@lineus
Mar 20 2018 23:00
@AngelMunoz in the issue you had what looked like references in location and industries, but here they're just strings. I'll reapproach my example either in a couple hours when my kids are in bed or first thing in the morning (4am EDT)
Angel Daniel Munoz Gonzalez
@AngelMunoz
Mar 20 2018 23:11
quite early haha, sure I'm not in a rush on that actually it just seemed weird the fact that if a location already existed and I tried to save a company with that location, it threw a unique constraint error
steadysupply
@steadysupply
Mar 20 2018 23:51
@lineus thanks man i'll have a look
btw, what version of mongoose are you using?