简体   繁体   中英

How can I get the value of this array in MongoDB?

How can I get the value of this array

const { model, Schema} = require("mongoose")

let ticketSchema = new Schema({
    GuildID : String,
    MembersID: [String],
    TicketID: String,
    ChannelID: String,
    Closed: Boolean,
    Locked: Boolean,
    Type: String,
    Claimed: Boolean,
    ClaimedBy: String,
    Open:Boolean,
    OpenBy:String,
})

module.exports = model("Ticket", ticketSchema);

Example:

{
  _id: new ObjectId("63d8340e0a415466f7ad55f0"),
  GuildID: '1066373193767592047',
  MembersID:
  [0]:811664013313310720
  TicketID: '2',
  ChannelID: '1069728239200780398',
  Closed: false,
  Locked: false,
  Type: 'Hola',
  Claimed: false,
  Open: false,
  OpenBy: '811664013313310720',
  __v: 0
}

How can I get the value of MembersID:[0]:811664013313310720 in this case it would be 811664013313310720 I need it to check on a system

Try methods with findOne, Find

I assume you use MongoDB Driver and the following syntax can extract the property MembersID

data = await dbo.collection(COLLECTION).find({},{projection:MembersID:1}).toArray()

MongoDB Drivers → Node.js

const MongoClient = require('mongodb').MongoClient;
const config = require('../config/mongodb'); // Your Own Connections Details
let db, dbo;
  const COLLECTION = 'Ticket';
  db = await MongoClient.connect(config.url, config.connection);
  dbo = await db.db(config.database);
  let data = [];
  const options = {
    projection: { MembersID: 1 }
  };
data = await dbo.collection(COLLECTION).find({}, options).toArray();
data[0].MembersID // Access the Array

Mongoose

data = await dbo.collection('Ticket').find({}).exec();

Point to Note

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM