When I apply "console.log" on an JS Object, the console output this thing :
I20170421-14:54:09.786(2)? Document {
I20170421-14:54:09.787(2)? _id: 'KQ7mdidtcxsQsqNjr',
I20170421-14:54:09.787(2)? name: 'eos test',
I20170421-14:54:09.787(2)? number: 69526,
I20170421-14:54:09.788(2)? part: 'bus',
I20170421-14:54:09.788(2)? active: true,
I20170421-14:54:09.789(2)? cron: 6,
What is this 'Document' ??? How I can remove to comapre this Object with the same without 'Document'...
I'm lost !
This document is an output of 'findOne'. I use Meteor with some packages (mongo#1.1.16, aldeed:simple-schema, aldeed:collection2, mdg:validated-method, mdg:validation-error, dburles:collection-helpers).
Thanks :)
You could compare manually (obj2.field1 === obj2.field1) or just use JSON.stringify in both objects and compare the resulting string.
But in your case I think you are not retrieving the object from mongo as you should.
Use fetch() after the query: DocumentCollection.find({}).fetch() or just .findOne() instead.
Also, if you want to compare 2 Meteor documents, you can use:
_.isEqual(doc1, doc2) (underscore)
Related
This is my problem : I have a document ( let's call it root) containing an array of another documents (stick), that contain another array of another documents (leaf).
Or simply said : root{ stickChain[leaveschain1[ leaf1, leaf2],leaveschain2[ leaf1, leaf2] ]}
I have access to root _id, stick _id, or whatever it is needed to identify each document.
Basically, the best result I've come so far is, when creating my leaves documents, is to store then at the same level tha sticks, or in another word I've come to create an array of leaves in root.
I'm working in javascript and using mongoose
This is the line I've used:
db.collection('root').findOneAndUpdate({ _id: root.id, "stick._id":expTemp._id },{stickChain:{$push: {"leavechain.$": leaf}}})
And I this gives me this result : root{ leavesChain[leaf1,leaf2], stickchain[1,2,3] }
I've come across something new to me (since Mongodb 3.6 there is a new way of handling array of arrays), here is what I've tried :
try{ db.collection('root').findOneAndUpdate(
{ _id: root.id, "stick._id":expTemp._id },
{$push: {"stickChain.$[xpc].leavesChain.$[qac]": leaf}},
{ arrayFilters: [ { "xpc._id": user._id } , { "qac._id": expTemp._id } ]})}
UPDATE
try{ db.collection('root').findAndModify(
{$push: {"root.$[cv].stickChain.$[xpc].leavesChain.$[qac]": leaf}},
{ arrayFilters: [ {"cv._id":user.cv} ,{ "xpc._id": user._id } , { "qac._id": expTemp._id } ],
upsert:true})
.catch(error => console.error(error))
}catch{}
And this gives me a new kind of error : MongoError: Either an update or remove=true must be specified
The thing is that I'm not familiar with how to do it, while I know what I want to do: I want to insert a "leaf" into a specific array in a document fetched in MongoDB. Maybe not the best practice, so any hint are welcome :)
I've splitted my dument like this :
root[stickChain _id]
stick[leavesChain[leaf]]
Thanks to Andrey Popov for his explications
I have a read-only object that is returned by GraphQL (vue-apollo) query, the result which is read-only looks something like this:
result: {
id: 'yh383hjjf',
regulations: [{ title: 'Test', approved: false}]
})
I want to bind this to a form and be able to edit/update the values in the regulations array and save it back to the database.
at the moment when I try to edit I get the error below:
Uncaught TypeError: "title" is read-only
I tried cloning the result returned by the database using object.assign
//target template
const regulatoryApprovals = {
id: null,
regulations: [{ title: null, approved: null}]
})
regulatoryApprovals = Object.assign(regulatoryApprovals, result, {
regulations: Object.assign(regulatoryApprovals.regulations, result.regulations)
})
but this didn't work.
Does anyone know how I can properly clone the result?
regulatoryApprovals= Object.assign(regulatoryApprovals, ... indicates the problem because regulatoryApprovals is modified with Object.assign, so it would need no assignment.
Read-only regulatoryApprovals object needs to be cloned. regulations is an array and won't be merged correctly with Object.assign, unless it's known that array elements need to be replaced. It should be:
regulatoryApprovals = {
...regulatoryApprovals,
...result,
regulations: [...regulatoryApprovals.regulations, result.regulations]
}
Where { ...regulatoryApprovals, ... } is a shortcut for Object.assign({}, regulatoryApprovals, ...).
I have a bug that i can't resolve because for the first time it happen to me.
here is my query :
Pack.find(
{idclient: clientId }
)
.populate({
path: 'cards',
options: { sort: { 'position': 1 } }
})
. exec(function(err,pack){
if(err){
console.log(err);
}else{
///
// here are my logs
callback(pack);
}
});
When i try console.log(pack), i can see a strange return with \n
{ __v: 1,\n _id: 5596a859240cbd3832123b27,\n grouped: 0,\n idclient: \'4Z8OrisV2AMLZn_lAAAA\',\n matId: 5596a859240cbd3832123b26,\n reversed: 0,\n roomId: 5596a859e37d7e7099cec1e6,\n shuffled: 0,\n type: \'hand\',\n cards: [ 5596a859240cbd3832123b28, 5596a85c240cbd3832123b5d ],\n date: Fri Jul 03 2015 17:20:57 GMT+0200 (CEST),\n iscut: 0 }
usually, i can see a nice formated Json Object.
So, when i try :
console.log(pack.property) => undefined ...
anyone has had this problem ?
Thanks
Two parts to this one ...
First, the callback from a Mongoose find returns an array ... findOne will return a single object.
As far as the new lines go, mongoose documents have a toString() helper for console.log. It is likely adding the newlines for readability. Wrap the output in JSON.stringify (ie. console.log(JSON.stringify(pack))) prior to calling console.log and you will see the document as a string without the newlines. -http://mongoosejs.com/docs/api.html#document_Document-toString
find() returns an array, so use findOne(), thanks to Adam Wysocki.
Sometimes i'm stupid developper .
As Told Model.find() generates an array so here is how I approach the situation:
Kitten.find(function (err, kittens) {
if (err) return console.error(err);
kittens.forEach(function(kitten){
console.log(kitten.name);
});
});
This seems to me the most clear way to access the properties
I've got an issue reading a nested array from JSON(BSON from MongoHQ) using Node and Angular.
JSON snippet: http://pastie.org/9305682. Specifically look for the edges array.
Mongoose model: http://pastie.org/9305685
Basically I call the character from the DB and then attempt to log it to the console with
console.log(char); before sending it back to the angular call with res.json(char); 'char' is the returned character from the databased saved as my mongoose model.
Attempting to log the character to the console. I get everything looking normal except for the portions with the nested "effects" arrays. Anywhere they show up I receive the following:
edges:
[ { name: 'Super Hacker', notes: '', effects: [Object] },
{ name: 'Witty', notes: '', effects: [Object] },
{ name: 'Attractive', notes: '', effects: [Object] },
{ name: 'Encyclopedic Memory',
notes: 'Prereq: d8 Smarts',
effects: [Object] },
{ name: 'Daywalker', notes: '', effects: [Object] },
{ name: 'Tough', notes: '', effects: [Object] } ],
From here if I try to call it with:
From NodeJS - console.log(char[0].edges[0].effects[0].type); - Returns undefined.
From Angular View - {{cur_char.edges[0].effects[0].type}} - Displays nothing.
Thanks in advance for the help. Let me know if I can provide more in.
I think what you're asking is how to see more depth to the object in the console output. You can use util.inspect to print out more information:
console.log(util.inspect(char, { depth: 5 }));
By default util.inspect only goes to a depth of 2 which explains why you can only see the contents of the array (1) and the primitive properties of each element in the array (2).
See: http://nodejs.org/api/util.html#util_util_inspect_object_options
I need to access the "State" value from the following array --
data =
{
Images:
[
{
ProductCodes: [],
BlockDeviceMappings: [Object],
Tags: [],
ImageId: 'ami-75301c',
ImageLocation: '54696560/Test Image 3',
State: 'available',
VirtualizationType: 'pavirtul',
Hypervisor: 'xen'
}
],
requestId: '2eb809d3-7f82-4142-b5d1-6af3'
}
When I try data.Images["State"] or data.Images.State I get undefined.
Thanks
Images maps to an array which stores objects, so you have to specify the index of the item you want. Try data.images[0]["State"].
You can access like this:
data.Images[0].State
Or even:
data.Images[0]['State']
Access the state with data.image[0].state. Your method was wrong because inside the image, you need an index within the two square bracket, the image property is an array.